2011-07-10 22 views
3

他のオブジェクトからプロパティを継承する関数/呼び出し可能オブジェクトを作成する方法はありますか?これは__proto__で可能ですが、そのプロパティは廃止予定/非標準です。これには標準に準拠した方法がありますか?継承されたプロパティを持つ関数(呼び出し可能)オブジェクト

/* A constructor for the object that will host the inheritable properties */ 
var CallablePrototype = function() {}; 
CallablePrototype.prototype = Function.prototype; 

var callablePrototype = new CallablePrototype; 

callablePrototype.hello = function() { 
    console.log("hello world"); 
}; 

/* Our callable "object" */ 
var callableObject = function() { 
    console.log("object called"); 
}; 

callableObject.__proto__ = callablePrototype; 

callableObject(); // "object called" 
callableObject.hello(); // "hello world" 
callableObject.hasOwnProperty("hello") // false 
+0

が重複する可能性(私はこれをしなければならなかった場合、私はまたidhelloと同様のものクロージャ内部の代わりに使用してグローバルを非表示にします)プロトタイプ?](http://stackoverflow.com/questions/548487/how-do-i-make-a-callable-js-object-with-an-arbitrary-prototype) – CMS

答えて

1

doesn't seem to be possibleは標準的な方法です。

普通のコピーを代わりに使用することはできませんか?

function hello(){ 
    console.log("Hello, I am ", this.x); 
} 

id = 0; 
function make_f(){ 
    function f(){ 
      console.log("Object called"); 
    } 
    f.x = id++; 
    f.hello = hello; 
    return f; 
} 

f = make_f(17); 
f(); 
f.hello(); 

g = make_f(17); 
g(); 
g.hello(); 

[Iは任意で呼び出し可能JSオブジェクトを作るにはどうすればよいの

関連する問題