Gre*_*een 51 javascript oop prototype
我有这样的例子.
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();
Run Code Online (Sandbox Code Playgroud)
我想更改代码,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
Run Code Online (Sandbox Code Playgroud)
为什么不能以这种方式更改构造函数中的代码?
Jua*_*des 80
您无法通过重新分配来更改构造函数 prototype.constructor
发生的事情是Rabbit.prototype.constructor指向原始构造函数(function Rabbit(){...})的指针,以便"类"的用户可以从实例中检测构造函数.因此,当你尝试做:
Rabbit.prototype.constructor = function Rabbit() {
this.jumps = "no";
};
Run Code Online (Sandbox Code Playgroud)
您只会影响依赖于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
});Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
您可以看到,在上面的代码中,我们必须修复构造函数属性,因为它有时用于在您只有实例时创建实例化对象.但它不会影响实际的构造函数.请参阅我关于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;
Run Code Online (Sandbox Code Playgroud)
请注意,这不会影响已复制该引用的代码,例如:
const myRefRabbit = Rabbit
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31906 次 |
| 最近记录: |