【发布时间】:2013-07-25 12:55:23
【问题描述】:
在下面这段代码中,如何访问B.prototype.log 内部的A.prototype.log?
function A() {}
A.prototype.log = function () {
console.log("A");
};
function B() {}
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
B.prototype.log = function () {
//call A.prototype.log here
console.log("B");
};
var b = new B();
b.log();
我知道我可以写A.prototype.log.call(this),但我想也许有一种更优雅的方式,让我以相对的方式调用它,比如“调用原型链中下一个更高实例的方法'log' ”。这样的事情可能吗?
【问题讨论】:
-
实际上,
A.prototype.log.call(this)正是我搜索的内容。谢谢!
标签: javascript inheritance overriding prototype