[英]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现在,我有三个问题
What is @class (or @ in general in js)?什么是@class(或 js 中的一般@)? var User = /** @class */ (function () {
classes are in javascript as well?类也在 javascript 中? so why don't typescript transform it into JS class那么为什么不打字稿将其转换为 JS 类
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类有什么区别
Answering your questions:回答您的问题:
@Class is a kind of annotation/comment that nothing as to do with the standard. @Class 是一种注释/注释,与标准无关。
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.与新规范相比,这种编写“类”(记住它们不存在)的方法有点困难。
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.我正在回答以完成第三个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.