【问题标题】:Angular2, which language should be used as the preferred one [closed]Angular2,应该使用哪种语言作为首选[关闭]
【发布时间】:2016-11-23 06:57:05
【问题描述】:

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

由于 type script 和 ES6/ES5 的语法完全不同,应该使用哪一种?是什么原因?

谢谢

【问题讨论】:

标签: typescript angular ecmascript-6 ecmascript-5


【解决方案1】:

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

没有。 TypeScript 添加了额外的语法。这是一张图片:

哪个应该是首要使用的

打字稿

是什么原因

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

更多

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

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

【讨论】:

  • 非常大的圈子 :) +1
  • 它的生态系统很大 ;)
【解决方案2】:

正如@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());
      }
    }
    

【讨论】:

    猜你喜欢
    • 2011-06-14
    • 2018-04-09
    • 1970-01-01
    • 2012-11-06
    • 2010-11-05
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多