最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript classes vs TypeScript classes - Stack Overflow

programmeradmin1浏览0评论

I am new to TypeScript and JavaScript classes!

I was learning TypeScript where I created something as simple as this

class User {
  name: string;
  email: string;

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

let newUser = new User("Rohit Bhatia", "[email protected]");

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", "[email protected]");

Now, I have three questions

  1. what is @class (or @ in general in JavaScript)? var User = /** @class */ (function () {

  2. classes are in JavaScript as well? So why doesn't TypeScript transform them into JS classes?

  3. in TS class we can do something like this

    class User { name: string; email: string;

but can't we do something like this in JavaScript? Or what is the difference between JS classes and TS classes?

I am new to TypeScript and JavaScript classes!

I was learning TypeScript where I created something as simple as this

class User {
  name: string;
  email: string;

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

let newUser = new User("Rohit Bhatia", "[email protected]");

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", "[email protected]");

Now, I have three questions

  1. what is @class (or @ in general in JavaScript)? var User = /** @class */ (function () {

  2. classes are in JavaScript as well? So why doesn't TypeScript transform them into JS classes?

  3. in TS class we can do something like this

    class User { name: string; email: string;

but can't we do something like this in JavaScript? Or what is the difference between JS classes and TS classes?

Share Improve this question edited Feb 19, 2022 at 0:42 Paolo 21.1k21 gold badges76 silver badges121 bronze badges asked Feb 23, 2019 at 12:06 AlwaysblueAlwaysblue 11.8k44 gold badges139 silver badges252 bronze badges 1
  • 4 Classes weren't added to JS until the ES2015 version of the spec - the code outputted by the TypeScript compiler is functionally identical to a JS class (as JS classes are just syntax sugar for functions), but will work on browsers that do not support them. If you set the TypeScript compiler to output ES2015 code via the target option, I believe the class will be output as a proper JS class. – Joe Clay Commented Feb 23, 2019 at 12:12
Add a comment  | 

3 Answers 3

Reset to default 6

Answering your questions:

  1. @class is a kind of annotation/comment that nothing as to do with the standard.

  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. That way to code "classes" (remember that they aren't there) is a bit harder compared to new specifications.

  3. See answer 2 too. Also:

Since the modern JavaScript ECMAScript 6 specification (ES6), now JavaScript has classes. TypeScript is a kind of evolution of ES6. In ES6, it would be like this:

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

var newUser = new User('Rohit Bhatia', '[email protected]');
  1. /** @class */ is just a comment
  2. TypeScript's default target is ES5 so it transpiles to JS code that can execute on browsers as old as IE11. If you set ES6 as target, you'll notice that JS classes will be used
  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

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.
  2. As said in the other 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.
  3. The ES6 classes are similar to TypeScript classes, but TypeScript also has public/protected/private modifiers, that there aren't in ES6 classes. 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).
发布评论

评论列表(0)

  1. 暂无评论