es6类真的是语义糖吗? [英] Are the es6 classes really semantic sugar?

查看:115
本文介绍了es6类真的是语义糖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它们只是语义糖,我如何在es5中获得以下es6脚本的相同结果?

If they are just semantic sugar how can I get the same result of the following es6 scripts in es5?

class MyFunc extends Function{}
new MyFunc('alert("hi guys")')()

class MyArr extends Array{}
var myarr = new MyArr(1,2,3,4)
myarr[2] = "a value"
myarr.push("an other value")


推荐答案

不,他们只是 语法糖。他们可以完成类模式在ES5中所做的所有事情,但也可以做更多。

No, they are only mostly syntactic sugar. They can do all the things that the class pattern did in ES5, but also more than that.

对对象实例化的细节,特别是在子类中的细节进行了彻底检查,现在允许像你的问题那样继承内部类的子类,如 Function Array 。这在ES5中是不可能的。有关详细信息,请查看什么是new.target? super()在构造函数中实际做了什么?子类是什么构造函数对象是否继承自?

The details of how objects are instantiated, especially in subclasses, was overhauled, and now allows to subclass the builtins like Function and Array as in your question. This was not possible in ES5. For details, have a look at What is "new.target"?, What does super() actually do in constructor function? and What do subclass constructor objects inherit from?.

这篇关于es6类真的是语义糖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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