以下是我的类,它的原型值没有改变,(我正在 Chrome 控制台上工作)
class Rectangle {
constructor(length, width) {
this.length = length;
this.width = width;
}
getArea() {
return this.length * this.width;
}
static create(length, width) {
return new Rectangle(length, width);
}
}
我正在将类的原型更改为 null
Rectangle.prototype= null
当我尝试访问更改后的原型时,该值与矩形的“getArea”原型属性保持相同的“对象”
但在 ES5 中,原型值发生了变化。
In ES6, the .prototype
property of class
es is not writable and not configurable1. Use strict mode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode where you do the assignment and you'll get ReferenceError
exception "Invalid assignment in strict mode".
If you want to overwrite the .prototype
(which is a very bad idea in general), you'll have to use Object.defineProperty(Rectangle, "prototype", {value: …})
.
1: See §14.5.14 ClassDefinitionEvaluation http://www.ecma-international.org/ecma-262/6.0/#sec-runtime-semantics-classdefinitionevaluation, step 16: Perform MakeConstructor(F, writablePrototype=false, prototype=proto).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)