Angular2,应该使用哪种语言作为首选语言

时间:2016-07-20 07:05:24

标签: typescript angular ecmascript-6 ecmascript-5

我是Angular 2的新手,我了解到开发人员可以使用类型脚本,ES6和ES5进行开发,我知道类型脚本是ES6和ES5的超集。

因为类型脚本和ES6 / ES5之间的语法完全不同,哪一个应该是最常用的?是什么原因?

感谢

2 个答案:

答案 0 :(得分:7)

  

因为类型脚本和ES6 / ES5之间的语法完全不同

没有。 TypeScript添加其他语法。这是一张图片:

  

哪一个应该是最常用的

TypeScript

  

是什么原因

更好的IDE工具和记录代码的方法。

更多

我对TypeScript:https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html

的看法

此视频演示了JavaScript和TypeScript之间的工具差异:https://www.youtube.com/watch?v=gmKXXI_ck7w

答案 1 :(得分:1)

正如@basarat所说,TypeScript是ES6的超集。这意味着只有在需要TypeScript时才能使用ES6。这意味着TypeScript中本身支持ES6模块,类支持和反引号。

话虽这么说,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());
      }
    }
    
相关问题