我看到this article on polymorphic callable objects,并试图让它工作,但它似乎并不是真正的多态,或者至少他们不尊重原型链。javascript“polymorphic callable objects”
此代码打印undefined
,而不是"hello there"
。
此方法不适用于原型,还是我做错了什么?
var callableType = function (constructor) {
return function() {
var callableInstance = function() {
return callableInstance.callOverload.apply(callableInstance, arguments);
};
constructor.apply(callableInstance, arguments);
return callableInstance;
};
};
var X = callableType(function() {
this.callOverload = function(){console.log('called!')};
});
X.prototype.hello = "hello there";
var x_i = new X();
console.log(x_i.hello);
我刚刚对你的肖像和名字印象深刻。我想他的名字舒仁周。 – xis