私はJavaScriptで関数を作成し、その関数にさらにメソッドと属性を追加できるのが好きです
myInstance = function() {return 5}
myInstance.attr = 10
これらのオブジェクトを生成するクラスを作成したいと思います。Function 基底クラスから継承する必要があると思います。
言い換えれば、私はしたい:
var myInstance = new myFunctionClass()
var x = myInstance()
// x == 5
しかし、myFunctionClass の作成方法がわかりません。次のことを試しましたが、うまくいきません。
var myFunctionClass = function() {Function.call(this, "return 5")}
myFunctionClass.prototype = new Function()
myInstance = new myFunctionClass()
myInstance()
// I would hope this would return 5, but instead I get
// TypeError: Property 'myInstance' of object #<Object> is not a function
ここにあるより複雑な(そしてより適切な?)継承方法も試しました:How to "properly" create a custom object in JavaScript? 、もう運がありません。また、node.js にある util.inherits(myFunctionClass, Function) を使用してみました。まだ運がない
私は Google を使い果たしたので、基本的または明白な何かが欠けているに違いないと感じています。助けていただければ幸いです。