我試圖讓這個:原型屬性的功能
a = new Foo(name); // a is function
a(); // returns a result
a.set(); // returns another result
我上面這樣實施:
function Foo (name) = {
val = name;
ret = function() { return val; }
ret.set = function (v) { return val = v; }
return ret;
}
然後,我想不富的多個實例創建方法'set',但通過prototype屬性分享。但是,我所做的所有實驗都沒有效果。它僅適用於對象,不適用於功能。即使下面的代碼不起作用:
foo = function() { return 'a'; }
foo.foo = function() { return 'foo'; }
foo.prototype.bar = function() { return 'bar'; }
foo(); // a
foo.foo(); // foo
foo.bar(); // Uncaught TypeError: Object function() { return 'a'; } has no method 'bar'
是的,我發現[類似的問題](http://stackoverflow.com/questions/16596117/can-you-create-functions-with-custom-prototypes-in-javascript)。答案與此類似 - 現在有辦法做到這一點。 – Dmitry