我看到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