我有这样的例子。
function Rabbit() {
var jumps = "yes";
};
var rabbit = new Rabbit();
alert(rabbit.jumps); // undefined
alert(Rabbit.prototype.constructor); // outputs exactly the code of the function Rabbit();
我想更改代码,Rabbit()
以使其var jumps
公开。我这样做:
Rabbit.prototype.constructor = function Rabbit() {
this.jumps = "no";
};
alert(Rabbit.prototype.constructor); // again outputs the code of function Rabbit() and with new this.jumps = "no";
var rabbit2 = new Rabbit(); // create new object with new constructor
alert(rabbit2.jumps); // but still outputs undefined
为什么无法以这种方式更改构造函数中的代码?
您不能通过重新分配给来更改构造函数 prototype.constructor
发生的事情是Rabbit.prototype.constructor
指向原始构造函数(function Rabbit(){...}
)的指针,因此“类”的用户可以从实例中检测到构造函数。因此,当您尝试执行以下操作时:
Rabbit.prototype.constructor = function Rabbit() {
this.jumps = "no";
};
您只会影响依赖于prototype.constructor
从实例动态实例化对象的代码。
调用时new X
,JS引擎不引用X.prototype.constructor
,而是将X
用作构造函数和X.prototype
新创建的对象的原型,而忽略X.prototype.constructor
。
解释此问题的一个好方法是自己实施new
操作员。(克罗克福德会很高兴,不再有新事物了;)
// `new` emulator
//
// Doesn't reference `.constructor` to show that prototype.constructor is not used
// when istantiating objects a la `new`
function make(ctorFun, argsArray) {
// New instance attached to the prototype but the constructor
// hasn't been called on it.
const newInstance = Object.create(ctorFun.prototype);
ctorFun.apply(newInstance, argsArray);
return newInstance;
}
// If you create a utility function to create from instance, then it uses the
// inherited `constructor` property and your change would affect that.
function makeFromInstance(instance, argsArray) {
return make(instance.constructor, argsArray);
}
function X(jumps) {
this.jumps = jumps;
}
// Flip the constructor, see what it affects
X.prototype.constructor = function(jumps) {
this.jumps = !jumps;
}
const xFromConstructorIsGood = make(X, [true]);
const xFromInstanceIsBad = makeFromInstance(xFromConstructorIsGood, [true]);
console.log({
xFromConstructorIsGood,
xFromInstanceIsBad
});
JS中的继承
帮助JS继承的库实现继承并确实依赖于prototype.constructor
以下内容:
function extend(base, sub) {
function surrogateCtor() {}
// Copy the prototype from the base to setup inheritance
surrogateCtor.prototype = base.prototype;
sub.prototype = new surrogateCtor();
// The constructor property is set to the base constructor
// with the above trick, let's fix it
sub.prototype.constructor = sub;
}
您可以在上面的代码中看到,我们必须修复构造函数属性,因为当您只有一个实例时,有时会使用它来创建实例化对象。但这不会影响实际的构造函数。请参阅我关于JS继承的文章http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html
如何重新定义构造函数如果您确实要重新定义构造函数,请执行
// If Rabbit had any custom properties on it
// (or static properties as some call it), they would not be copied, you'd have to do that manually using getOwnPropertyNames
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
var oldProto = Rabbit.prototype;
Rabbit = function() {...};
Rabbit.prototype = oldProto;
请注意,这不会影响已经复制该引用的代码,例如:
const myRefRabbit = Rabbit
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句