是javascript的原型模式的es6类只是句法糖? [英] are es6 classes just syntactic sugar for the prototypal pattern in javascript?

查看:75
本文介绍了是javascript的原型模式的es6类只是句法糖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩ES6之后,我真的开始喜欢新的语法和功能,但我有一个关于课程的问题。

After playing with ES6 I've really started to like the new syntax and features available, but I do have a question about classes.

是新的ES6类只是古老原型的句法糖?或者幕后还有更多的事情吗?即:

are the new ES6 classes just syntactic sugar for the old prototypal pattern? or is there more going on here behind the scenes? ie:

class Thing {
   //... classy stuff
}

vs:

var Thing = function() {
  // ... setup stuff
};

Thing.prototype.doStuff = function() {}; // etc


推荐答案


新的ES6类只是原型样式的语法糖?

Are the new ES6 classes just syntactic sugar for the old prototypal pattern?

是的,它们只是一个方便的语法,语义相同。

Yes, they are simply a convenience syntax, the semantics are identical.


JavaScript类在ECMAScript 6中引入,并且是JavaScript现有的基于原型的继承的语法
糖。
类语法没有向JavaScript引入新的面向对象的继承
模型。 JavaScript类提供了一个更简单和
更清晰的语法来创建对象并处理继承。

JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.

来源

以下短小

class Thing {
   someFunc() {}
}

console.log("someFunc" in Thing.prototype); // true

这篇关于是javascript的原型模式的es6类只是句法糖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆