【发布时间】:2017-05-31 03:38:04
【问题描述】:
我正在做一个继承的例子。我想访问abc 和pqr 的所有属性,所以我使用了Object.create。但是,在调用 getr() 函数时,我无法获得 r 的值。我做错了什么?
function abc() {
this.a = 3;
}
abc.prototype.getA = function() {
return this.a
}
function pqr() {
abc.call(this);
this.r = 3;
}
pqr.prototype.getr = function() {
return this.r
}
pqr.prototype = Object.create(abc.prototype);
var n = new pqr();
console.log(n.getr());
【问题讨论】:
-
您将
getr()附加到pqr的原型上,然后覆盖 该原型,因此它不起作用。
标签: javascript jquery inheritance