Angular2,应该使用哪种语言作为首选语言
Angular2, which language should be used as the preferred one
我是新手Angular2,了解到开发者可以使用type script,ES6和ES5进行开发,我知道type script是ES6和ES5的超集。
由于 type script 和 ES6/ES5 的语法完全不同,应该优先使用哪一个?原因是什么?
谢谢
since the syntax is totally different between type script and ES6/ES5
没有。 TypeScript 添加了 附加语法 。这是一张图片:
which one should be the prime one to use
打字稿
what is the reason
更好的IDE 工具和记录代码的方法。
更多
我对为什么使用 TypeScript 的看法:https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html
此视频演示了 JavaScript 和 TypeScript 之间的工具差异:https://www.youtube.com/watch?v=gmKXXI_ck7w
正如@basarat 所说,TypeScript 是 ES6 的超集。这意味着只有在需要 TypeScript 时才可以使用 ES6。这意味着 ES6 模块,class 支持,TypeScript 原生支持反引号。
话虽如此,我认为 TypeScript 的三件很酷的事情是:
类型支持和类型检查。这个强大的功能可以让您在执行应用程序之前确定您没有不存在的东西。
export class SomeClass {
someProperty: string; // property of type string
someProperty: SomeOtherClass; // property of type SomeOtherClass
constructor(param:string) {
this.someProperty = new SomeOtherClass();
// SomeOtherClass must have a property named value of type
// string. Otherwise, you will have some compilation errors.
this.someProperty.value = param;
}
}
全面支持装饰器。使用 ES6,您不能在构造函数/方法参数中使用它。在 Angular2 中,这很重要,主要是因为基于构造函数的依赖注入
// With ES6 only
@Injectable()
export class SomeClass {
constructor(service) {
this.service = service;
}
get parameters() {
return [[SomeOtherClass]];
}
}
// With TypeScript
@Injectable()
export class SomeClass {
constructor(private service:SomeOtherClass) {
}
}
接口支持。虽然它仅用于设计时(而不是运行时),但接口可用于指定元素的契约
export interface SomeData {
id:string;
name:string;
}
@Injectable()
export class SomeHttpService {
constructor(private http:Http) {
}
executeHttpRequest():Observable<SomeData[]> {
return this.http.get('http://...')
.map(res => <SomeData[]>res.json());
}
}
我是新手Angular2,了解到开发者可以使用type script,ES6和ES5进行开发,我知道type script是ES6和ES5的超集。
由于 type script 和 ES6/ES5 的语法完全不同,应该优先使用哪一个?原因是什么?
谢谢
since the syntax is totally different between type script and ES6/ES5
没有。 TypeScript 添加了 附加语法 。这是一张图片:
which one should be the prime one to use
打字稿
what is the reason
更好的IDE 工具和记录代码的方法。
更多
我对为什么使用 TypeScript 的看法:https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html
此视频演示了 JavaScript 和 TypeScript 之间的工具差异:https://www.youtube.com/watch?v=gmKXXI_ck7w
正如@basarat 所说,TypeScript 是 ES6 的超集。这意味着只有在需要 TypeScript 时才可以使用 ES6。这意味着 ES6 模块,class 支持,TypeScript 原生支持反引号。
话虽如此,我认为 TypeScript 的三件很酷的事情是:
类型支持和类型检查。这个强大的功能可以让您在执行应用程序之前确定您没有不存在的东西。
export class SomeClass { someProperty: string; // property of type string someProperty: SomeOtherClass; // property of type SomeOtherClass constructor(param:string) { this.someProperty = new SomeOtherClass(); // SomeOtherClass must have a property named value of type // string. Otherwise, you will have some compilation errors. this.someProperty.value = param; } }
全面支持装饰器。使用 ES6,您不能在构造函数/方法参数中使用它。在 Angular2 中,这很重要,主要是因为基于构造函数的依赖注入
// With ES6 only @Injectable() export class SomeClass { constructor(service) { this.service = service; } get parameters() { return [[SomeOtherClass]]; } } // With TypeScript @Injectable() export class SomeClass { constructor(private service:SomeOtherClass) { } }
接口支持。虽然它仅用于设计时(而不是运行时),但接口可用于指定元素的契约
export interface SomeData { id:string; name:string; } @Injectable() export class SomeHttpService { constructor(private http:Http) { } executeHttpRequest():Observable<SomeData[]> { return this.http.get('http://...') .map(res => <SomeData[]>res.json()); } }