考虑这段代码:是否有可能检测父方法是否被覆盖?
function Parent(){};
Parent.prototype.myMethod = function()
{
return "hi!";
}
function Child(){};
Child.prototype = Object.create(new Parent());
var objChild = new Child();
//no override...
var output = objChild.myMethod();
alert(output); // "hi!"
//override and detect that in Parent.myMethod()
Child.prototype.myMethod = function()
{
var output = Parent.prototype.myMethod.call(this);
alert("override: " + output); // "hi!"
};
objChild.myMethod();
是否有可能以确定是否Parent.myMethod()
被称为“自然”,或通过“覆盖”在那种情况下返回别的东西吗?
'Object.create(new Parent());' - 哎!它应该是Object.create(Parent.prototype); – Bergi
@Bergi谢谢你的纠正!我是新的原型继承 –
是否有真正的用例?如果是这样,可能会有设计问题。 –