Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何处理';这';在typescript/js中?需要将Python代码转换为Typescript_Javascript_Python_Python 3.x_Typescript_This - Fatal编程技术网

Javascript 如何处理';这';在typescript/js中?需要将Python代码转换为Typescript

Javascript 如何处理';这';在typescript/js中?需要将Python代码转换为Typescript,javascript,python,python-3.x,typescript,this,Javascript,Python,Python 3.x,Typescript,This,我用python编写了这个简单的代码来创建一个小程序 纸牌游戏,我想做同样的使用打字脚本,但我是 在我的Typescript主课中,面临着一个关于“this”的大问题 这是原始python类: class deck: card_type=['Hearts ','Diamonds','Spades','Clubs'] card_rank=[2,3,4,5,6,7,8,9,10,'A','J','Q','K'] full_deck=[] def build_deck

我用python编写了这个简单的代码来创建一个小程序 纸牌游戏,我想做同样的使用打字脚本,但我是 在我的Typescript主课中,面临着一个关于“this”的大问题 这是原始python类:

class deck:
    card_type=['Hearts ','Diamonds','Spades','Clubs']
    card_rank=[2,3,4,5,6,7,8,9,10,'A','J','Q','K']
    full_deck=[]

    def build_deck(self):
        single={}
        for card in self.card_type:
            for n in self.card_rank:
                single={n: card}
                self.full_deck.append(single)
        shuffle(self.full_deck)


    def reshuffle (self):
        print('Re-shuffling again!')
        shuffle(self.full_deck)


    def choose_card(self):
        chosen=choice(self.full_deck)
        the_index= self.full_deck.index(chosen)
        self.full_deck.pop(the_index)

        return chosen

    def pick_hand(self, number_of_cards):
        hand=[]
        new_card={}

        for i in range(number_of_cards):
            new_card = self.choose_card()
            hand.append(new_card)

        return hand
在我的主要游戏文件中,我做了如下操作:

from classes import deck

deck1= deck()
deck1.build_deck()
my_hand=deck1.pick_hand(3)
compu_hand=deck1.pick_hand(3)

但当我尝试在类型脚本中创建类似的类时,我编写了以下内容:

export class deck {

  single_card: {
    cType: string;
    cNumber: any;
  };

  fullDeck: any[] = [];
  card_type=['Hearts ','Diamonds','Spades','Clubs'];
  card_rank=[2,3,4,5,6,7,8,9,10,'A','J','Q','K'];

  shuffle() {
    let counter = this.fullDeck.length;

    // While there are elements in the array
    while (counter > 0) {
        // Pick a random index
        let index = Math.floor(Math.random() * counter);

        // Decrease counter by 1
        counter--;

        // And swap the last element with it
        let temp = this.fullDeck[counter];
        this.fullDeck[counter] = this.fullDeck[index];
        this.fullDeck[index] = temp;
    }

    // return this.fullDeck;
  }


  buildDeck (){

    for (let t in this.card_type) {
      for ( let n in this.card_rank) {
        this.single_card.cType = this.card_type[t];
        this.single_card.cNumber = this.card_rank[n];
        this.fullDeck.push(this.single_card);
        console.log(this.single_card);
      }
    }
    // this.shuffle() 

  }

}
当我尝试使用主“ts”文件中的类时,如下所示:

import {deck} from './myclasses'

$(document).ready (function(){
    let deck1= new deck;
    deck1.buildDeck();
});
console.log调用返回相同的错误:

jQuery.Deferred异常:无法设置未定义的属性“cType” TypeError:无法设置未定义的属性“cType” 在甲板上(file:///run/media/Work/HTML_Porjects/Game_TS/built/myclasses.js:132:44)

它是如何定义的? 我需要做什么才能使Typescript代码像Python代码一样工作?


提前感谢…

错误仅表明
单卡
未定义,即:

class deck {

  single_card: {
    cType: string;
    cNumber: any;
  };

  // …
}
这将在TypeScript类上声明属性
single\u card
,以便编译器在您引用该类型对象的
single\u card
属性时(例如,在执行
This.single\u card
时)将接受该属性。但是,这样做实际上不会将该类型的对象指定给该对象。因此,对于已编译的JavaScript(类型信息已删除),该属性不存在,因为您从未分配给它。您可以通过查看

因此,您首先需要做的是为单卡分配一些内容,就像在Python版本中那样:

this.single_card = {}
但是,如果您实际查看Python版本,
single_card
不是该对象的成员,它实际上是没有任何意义的。您正在使用它来构造卡对象,然后将其添加到
fullDeck
数组中。在Python中,它是一个局部变量,因此在TypeScript版本中也应将其设置为局部变量:

buildDeck() {
  for (const t of this.card_type) {
    for (const n of this.card_rank) {
      const single_card = {
          cType: this.card_type[t],
          cNumber: this.card_rank[n]
      };
      this.fullDeck.push(single_card);
    }
  }
}
顺便说一句,您还希望在那里使用
for…of
循环,而不是
for…in

您还应该考虑正确键入
fullDeck
。现在它是一个由
any
组成的数组,因此它可以存储任何对象。但实际上,你要做的就是把对象放在里面,让它们看起来像单卡一样。因此,考虑为此声明一个类型:

interface SingleCard {
    cType: string;
    cNumber: any;
}
然后,您可以正确键入
fullDeck

fullDeck: SingleCard[] = [];

抱歉,我给出了一个我认为可能是大错特错的答案。但你似乎在奇怪地使用
单卡。您不是将其定义为数据类型吗?为什么它会像对象属性一样被使用?谢谢你的回复,我尝试了箭头功能,它不工作,但你的意思是什么?单张卡片是卡片组的属性,看起来不像。它不像所有其他属性一样显示在
中。它一定有语法上的错误。但我不懂打字稿;)你的答案是我几乎认为正在发生的事情,但缺乏了解实际TS语法的知识。很好的回答非常感谢您的详细回答,我同意您的观点,单卡不应该是对象的成员,但在我失败的尝试中,我将其移到了内部,但现在我将其移到了外部。至于你的建议,它实际上修复了错误,但出于某种原因,满牌是最后一张牌的52项!52个相同对象的副本!当我尝试在循环中console.log对象单张卡片时,我得到52张不同的卡片,但是列表满的卡片组只填充了最后一张卡片!为什么会发生这种情况?如果您继续推送相同的
单卡
对象,并且只更新其属性,那么所有对象都将是相同的。您应该在每次迭代中创建一个新对象,就像我在回答中所显示的那样(或者在分配其属性之前至少要做
single_card={}
)。非常感谢!终于成功了。。。我讨厌JS和打字脚本!不过,这不是特定于JS/TS的,如果在Python解决方案中使用实例成员,则会遇到完全相同的问题;)