打字稿-变量声明 [英] 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.
我只是在学习Angular 2和Typescript,所以正在研究以下教程:
I'm just learning Angular 2 and Typescript, so am working through this following tutorial:
https://angular.io/docs/ts/latest/tutorial/toh-pt4.html
在第三部分和第四部分之间,app.component.ts
中的heroes
变量的声明从以下内容更改:
Between part three and four, the declaration of the heroes
variable in the app.component.ts
changes from:
export class AppComponent {
heroes = HEROES;
}
收件人:
export class AppComponent {
heroes: Hero[];
}
我了解第一个将其设置为常量的heroes数组,但是为什么第二个使用冒号而不是仅将其设置为空数组?
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
- 名称=
heroes
- type =
Hero[]
表示Hero
的数组
- 不分配值将其保留为默认值
null
.
- name =
heroes
- type =
Hero[]
which means array ofHero
- without assigning a value which keeps it at default value
null
.
通过初始化,它看起来像
With initialization it would look like
heroes: Hero[] = [new Hero('Spidey'), new Hero('Batman')];
这篇关于打字稿-变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!