简体   繁体   English

Javascript 类与打字稿类

[英]Javascript classes vs typescript classes

I am new to TypeScript and Javascript classes!我是 TypeScript 和 Javascript 类的新手!

I was learning TypeScript where I created something as simple as this我正在学习 TypeScript,在那里我创建了一些像这样简单的东西

class User {
  name: string;
  email: string;

  constructor(name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}

let newUser = new User("Rohit Bhatia", "iro@gmail.com");

and this was given to me as equivalence这是给我的等价物

var User = /** @class */ (function () {
    function User(name, email) {
        this.name = name;
        this.email = email;
    }
    return User;
}());
var newUser = new User("Rohit Bhatia", "iro@gmail.com");

Now, I have three questions现在,我有三个问题

  1. What is @class (or @ in general in js)?什么是@class(或 js 中的一般@)? var User = /** @class */ (function () {

  2. classes are in javascript as well?类也在 javascript 中? so why don't typescript transform it into JS class那么为什么不打字稿将其转换为 JS 类

  3. In TS class we can do something like this在 TS 课上我们可以做这样的事情

    class User { name: string;类用户{名称:字符串; email: string;电子邮件:字符串;

but in Javascript we can't do something like this?但是在 Javascript 中我们不能做这样的事情吗? or what is the difference between JS classes and TS classes或者JS类和TS类有什么区别

  1. /** @class */ is just a comment /** @class */ 只是一个评论
  2. Typescript's default target is ES5 so it transpiles to JS code that can execute on browsers as old as IE11. Typescript 的默认目标是 ES5,因此它可以转换为可以在 IE11 等旧浏览器上执行的 JS 代码。 If you set ES6 as target, you'll notice that JS classes will be used如果你将 ES6 设置为目标,你会注意到将使用 JS 类
  3. ES5 way for writing classes is to use a function as a constructor, but the result when executing the code is exactly the same as ES6 classes ES5 写类的方式是使用函数作为构造函数,但是执行代码的结果和 ES6 的类完全一样

Answering your questions:回答您的问题:

  1. @Class is a kind of annotation/comment that nothing as to do with the standard. @Class 是一种注释/注释,与标准无关。

  2. In ES5 (let's say 'classic javascript') there are no classes, but there is a way to simulate their behaviour, also when Typescript code is transpiled to ES5.在 ES5(假设是“经典 javascript”)中没有类,但是有一种方法可以模拟它们的行为,当 Typescript 代码被转换为 ES5 时也是如此。 That way to code 'classes' (remember that they aren't there) is a bit harder compared to new specifications.与新规范相比,这种编写“类”(记住它们不存在)的方法有点困难。

  3. See answer 2 too.也见答案2。 Also:还:

Since the modern Javascript ECMAScript 6 specification (ES6), now Javascript has classes.从现代 Javascript ECMAScript 6 规范 (ES6) 开始,现在 Javascript 有了类。 Typescript is a kind of evolution of ES6. Typescript 是 ES6 的一种进化。 In ES6, it would be like this:在 ES6 中,它会是这样的:

class User {
  
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }
}

var newUser = new User('Rohit Bhatia', 'iro@gmail.com');

I'm answering to complete the third question.我正在回答以完成第三个问题。

  1. In Ecmascript standard, what is between /* and */ is just a comment, but it's possible that typescript transpiler or debugger use this annotations for something more.在 Ecmascript 标准中,/* 和 */ 之间的内容只是一个注释,但 typescript 转译器或调试器可能会使用此注释来做更多的事情。
  2. As said in the others answers, by default typescript transpile for Ecmascript 5 (ES5), for better compatibility with old browsers, and that it's a way to do something like class in ES5.正如其他答案中所说,默认情况下,Ecmascript 5(ES5)的打字稿转译,以更好地与旧浏览器兼容,并且这是一种在 ES5 中执行类之类的方法。
  3. The ES6 classes are similar to typescript classes, but typescript also has public/protected/private modifiers, that there aren't in ES6 classes. ES6 类类似于 typescript 类,但 typescript 也有 public/protected/private 修饰符,这是 ES6 类中没有的。 Also, in typescript you will have advantages by the stronger typing, which includes even better autocomplete in some editors (like Visual Studio Code, of course it depends of the editor).此外,在打字稿中,您将通过更强大的类型获得优势,其中包括在某些编辑器中甚至更好的自动完成功能(例如 Visual Studio Code,当然这取决于编辑器)。

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

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