javascript - 如果一个类扩展了 Object,Babel 真的不需要调用 super() 是真的吗?
问题描述
我注意到使用 Babel,如果我转译
class Rectangle {
a = 1;
}
使用 stage-0,则生成的代码有一个构造函数,但没有调用super()
但是如果代码改成:
class Rectangle extends Object {
a = 1;
}
那么转译的代码是:
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class Rectangle extends Object {
constructor(...args) {
super(...args);
_defineProperty(this, "a", 1);
}
}
原代码的版本 1 和 2 是不是真的一样?(所有类都扩展 Object)。那么如果版本 1 不调用super()
,看起来 Object 的构造函数什么也没做,那么版本 2 也没有理由调用它吗?
解决方案
原代码的版本 1 和 2 是不是真的一样?(所有类都扩展 Object)。
不,不完全是。让我们比较一下:
class Base {
}
和
class Sub extends Object {
}
确实,两者都Base.prototype
和Sub.prototype
用作Object.prototype
它们的原型,但这并不能使它们相同。两个区别:
Base
(构造函数)Object
用作其原型;Sub
使用Function.prototype
.Base
当您通过调用它时将创建new
对象;Sub
不会,它希望超类构造函数 (Object
) 这样做。这意味着它必须调用它。(构造函数被标记以表明它们是基构造函数还是子类构造函数,并且new
操作符的处理会根据。)
演示 #1(使用 ES2015+ JavaScript 引擎):
class Base {
}
class Sub extends Object {
}
// Both `prototype` properties inherit from `Object.prototype`
console.log(Object.getPrototypeOf(Base.prototype) === Object.prototype);
console.log(Object.getPrototypeOf(Sub.prototype) === Object.prototype);
// But the constructors themselves inherit from different things
console.log(Object.getPrototypeOf(Base) === Function.prototype);
console.log(Object.getPrototypeOf(Sub) === Object);
所以如果版本 1 不调用
super()
,看起来 的构造函数Object
没有做任何事情,那么版本 2 也没有理由调用它吗?
它必须调用它。这无法在本机 JavaScript (ES2015+) 中编译:
class Example extends Object {
constructor() {
this.foo = "bar";
}
}
console.log(new Example().foo);
如果您有extends
,则必须调用super
才能创建新对象。
答案顶部的MySub
可以编译,因为它没有显式构造函数,因此它获得了默认的子类构造函数 ( constructor(...args) { super(...args); }
)。但是Example
失败了,因为它有一个显式的构造函数但没有进行super
调用。
推荐阅读
- python - 在非数字列中搜索字符串值
- c# - 显示项目集合视图的先前位置
- javascript - 启用暗模式时JS更改浏览器选项卡图标
- javascript - 以时间间隔从服务器下载图像
- swift - 如何在 Swift 中获得侧边栏打开状态?
- retrofit - Retrofit Authenticator 调用但不工作
- logic - DP 和 DPLL 的可满足性产生不同的结果
- nmap - 为什么nmap“-ST”和“-SS”的结果不同
- c# - 反序列化来自统一的 Firebask SDK 错误的 JSON 响应
- java - 如果没有它可以工作,我应该将 RecyclerView 依赖项添加到 gradle 吗?