简体   繁体   English

打字稿 - 变量声明

[英]Typescript - variables declaration

I'm not sure if this is the place for this question, but I've been told code review is not the place for it. 我不确定这是否是这个问题的地方,但我被告知代码审查不适合它。

I'm just learning Angular 2 and Typescript, so am working through this following tutorial: 我刚学习Angular 2和Typescript,所以我正在学习以下教程:

https://angular.io/docs/ts/latest/tutorial/toh-pt4.html https://angular.io/docs/ts/latest/tutorial/toh-pt4.html

Between part three and four, the declaration of the heroes variable in the app.component.ts changes from: 在第三部分和第四部分之间, app.component.ts heroes变量的app.component.ts更改为:

export class AppComponent {
    heroes = HEROES;
}

to: 至:

export class AppComponent {
    heroes: Hero[];
}

I understand the first one sets it to a constant of heroes array but why does the second one use a colon and not just set it to an empty array? 我理解第一个将它设置为英雄数组的常量,但为什么第二个使用冒号而不只是将它设置为空数组?

Changing the second one to an = actually throws an expression expected error so basically I'm just trying to understand the differences between the two. 将第二个更改为=实际上会抛出表达式预期错误,所以基本上我只是想了解两者之间的差异。

heroes: Hero[];

doesn't set it to a value. 不会将其设置为值。 It just defines a property with 它只是定义了一个属性

  • name = heroes name = heroes
  • type = Hero[] which means array of Hero type = Hero[]表示Hero数组
  • without assigning a value which keeps it at default value null . 没有赋值使其保持默认值null

With initialization it would look like 初始化它看起来像

heroes: Hero[] = [new Hero('Spidey'), new Hero('Batman')];

The difference between the two is that the first one is javascript, you are assigning your heroes variable to the constant **HEROES*. 两者之间的区别在于第一个是javascript,你将你的英雄变量分配给常量** HEROES *。

The second one it's a typescript thing, you are saying that the heroes variable will be an array of heroes, basically you are defining an empty variable. 第二个是打字稿,你说英雄变量将是一个英雄数组,基本上你是在定义一个空变量。

It's like java or c# where you do something like public int myNumber on your class 它就像java或c#,你在类上使用public int myNumber

Your first understanding is right with 你的第一个理解是正确的

heroes = HEROES

Now, 现在,

heroes : Hero [ ];

Here Hero reprents class which holds certain properties. 在这里, Hero代表拥有某些属性的类。 by this line, you are telling typescript compiler that heroes variable will hold list of objects having type of Hero (custom or user defined type) . 通过这一行,您告诉typescript编译器, heroes variable将保存具有Hero类型(自定义或用户定义类型) list of objects In OOP world class is a user defined dataType and according that you can declare any variable with that Type. 在OOP中,world class是用户定义的dataType,根据class ,您可以使用该Type声明任何变量。

Please note , you can declare(:) any varaiable with Type, you cant assing(=) Type (in Typescript) but you can assign that Type of data to variable. 请注意,您可以使用Type声明(:)任何变量,您无法确定(=)Type(在Typescript中),但您可以将该that Type of data分配给变量。

So here you cant use = . 所以在这里你不能使用=

I hope this will help you... 我希望这能帮到您...

Just complementing @Günter Zöchbauer, 只是补充@GünterZöchbauer,

in class/interface declarations, : after a property name is used to define its type. 在类/接口声明中, :在使用属性名称定义其类型之后。

However, this might cause a little confusion when you're creating an object on-the-fly, which, in this case, : marks value assignment: 但是,当您在运行中创建对象时,这可能会引起一些混淆,在这种情况下, :标记值赋值:

interface MyInterface {
  myBooleanProperty: boolean = false;
}

// ... somewhere

let a = {
  myBooleanProperty: true
};

// and to add a little more fun

let b: MyInterface = {
  myBooleanProperty: true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM