Javascript protypal继承中的元对象有意义吗?
我在这里看到两种类型的原型继承函数创建模式Javascript protypal继承中的元对象有意义吗?,javascript,Javascript,我在这里看到两种类型的原型继承函数创建模式 Function.prototype.inherit = function( base ) { function Meta() {}; Meta.prototype = base; this.prototype = new Meta(); } 及 以前的实现似乎没有做任何额外的事情!中间有一个元函数,它的用例是什么 Meta函数的作用是通过调用构造函数来避免可能出现的副作用: function Base() { con
Function.prototype.inherit = function( base ) {
function Meta() {};
Meta.prototype = base;
this.prototype = new Meta();
}
及
以前的实现似乎没有做任何额外的事情!中间有一个元函数,它的用例是什么 Meta函数的作用是通过调用构造函数来避免可能出现的副作用:
function Base() {
console.log("This should not be called when inheriting!");
}
// If you use `new Base()` as a prototype, an unwanted message is logged
在ES5中,这是内置的:
在ES6中,您只需使用:
一个观察结果是,在节点6.10中,格式方法不再有效。
function Base() {
console.log("This should not be called when inheriting!");
}
// If you use `new Base()` as a prototype, an unwanted message is logged
this.prototype = Object.create(base.prototype);
class Derived extends Base {
// ...
}