Javascript 继承:子类型原型无法访问超类型的 property/function
Javascript inheritance: subtype prototype cannot visit super type's property/function
我有下面这段代码:
function me(){
this.age=30,
this.say=function(){return 'hello me'}
}
function child(){
this.hobby='sports'
}
child.prototype=new me();
var son=new child();
son.prototype=new me();
console.log(son.age);//30
console.log(son.__proto__.age);//30
console.log(son.constructor.prototype.age);//undefined
console.log(son.constructor.prototype.say())//exception
打印结果是,只有前2条日志打印出“30”,其他都打印出"undefined",最后一行甚至在运行时抛出异常。
(1) 我原以为他们都应该给我输出。为什么第 3 行打印“未定义”?
(2) 我希望 "proto" 和 "constructor.prototype" 具有相同的效果,但实际上不是。
son.constructor === me
和 me.prototype
没有属性 age
和 say
。请注意 son.__proto__.hasOwnProperty("constructor") === false
.
您正在将对象分配给本身没有构造函数 属性 的原型,这在访问 son.constructor 时会产生一些不直观的结果。它是 son.__proto__.__proto__.constructor
,这可能不是您想要的。
显示此行为的示例:
function A() {}
function B() {}
console.log("Automatically added: " + A.prototype.constructor.toString());
A.prototype = new B();
let a = new A();
console.log("a constructor: " + a.constructor.toString());
if (a.constructor
&& !a.hasOwnProperty("constructor")
&& !a.__proto__.hasOwnProperty("constructor"))
console.log("constructor property of a is further up the prototype chain!");
另请注意,您将 new me()
分配给两个不同的对象作为原型,这是不必要的,也可能不是您想要的。
最后这是一个工作示例:
function me(){
this.age=30,
this.say=function(){return 'hello me'}
}
function child(){
this.hobby='sports'
}
child.prototype=new me();
child.prototype.constructor = child;
var son=new child();
console.log(son.age);
console.log(son.__proto__.age);
console.log(son.constructor.prototype.age);
console.log(son.constructor.prototype.say())
我有下面这段代码:
function me(){
this.age=30,
this.say=function(){return 'hello me'}
}
function child(){
this.hobby='sports'
}
child.prototype=new me();
var son=new child();
son.prototype=new me();
console.log(son.age);//30
console.log(son.__proto__.age);//30
console.log(son.constructor.prototype.age);//undefined
console.log(son.constructor.prototype.say())//exception
打印结果是,只有前2条日志打印出“30”,其他都打印出"undefined",最后一行甚至在运行时抛出异常。
(1) 我原以为他们都应该给我输出。为什么第 3 行打印“未定义”?
(2) 我希望 "proto" 和 "constructor.prototype" 具有相同的效果,但实际上不是。
son.constructor === me
和 me.prototype
没有属性 age
和 say
。请注意 son.__proto__.hasOwnProperty("constructor") === false
.
您正在将对象分配给本身没有构造函数 属性 的原型,这在访问 son.constructor 时会产生一些不直观的结果。它是 son.__proto__.__proto__.constructor
,这可能不是您想要的。
显示此行为的示例:
function A() {}
function B() {}
console.log("Automatically added: " + A.prototype.constructor.toString());
A.prototype = new B();
let a = new A();
console.log("a constructor: " + a.constructor.toString());
if (a.constructor
&& !a.hasOwnProperty("constructor")
&& !a.__proto__.hasOwnProperty("constructor"))
console.log("constructor property of a is further up the prototype chain!");
另请注意,您将 new me()
分配给两个不同的对象作为原型,这是不必要的,也可能不是您想要的。
最后这是一个工作示例:
function me(){
this.age=30,
this.say=function(){return 'hello me'}
}
function child(){
this.hobby='sports'
}
child.prototype=new me();
child.prototype.constructor = child;
var son=new child();
console.log(son.age);
console.log(son.__proto__.age);
console.log(son.constructor.prototype.age);
console.log(son.constructor.prototype.say())