2016-12-06 3 views
0

事実の後にプロパティとしていくつかの関数を追加するのを怠る必要のあるオブジェクトのクラスがあります。これらの関数は、プロパティとしてアタッチされた関数を設定するためにインスタンス変数を参照する必要があるため、関数を作成して最初の関数呼び出しを転送するためにthunkを使用しました。簡単な例のために:私は残っているJSのサブオブジェクトプロパティへのアクセスのサンクを呼び出す方法は?

function Foo(val) { 
 
    this.val = val; 
 
} 
 

 
// simulates a library call which returns a complex object with methods 
 
// this library call requires access to some of the object information, 
 
// simulated by passing in `type` 
 
function buildWorkFunction(type) { 
 
    var f = function() { return this.val; }; 
 
    if (type === 'string') 
 
    f = function() { return this.val.length; }; 
 
    f.getType = function() { return type; }; 
 
    return f; 
 
} 
 

 
function addProperty(Base, prop) { 
 
    var thunk = function() { 
 
    // typeof is just a simple example of how buildWorkFunction could 
 
    // change based on this.val 
 
    this[prop] = buildWorkFunction(typeof this.val); 
 
    return this[prop].apply(this, arguments); 
 
    }; 
 
    thunk.getType = function() { 
 
    return 'TODO'; // <-- this is the part I need help with 
 
    }; 
 
    Base.prototype[prop] = thunk; 
 
} 
 

 
// simulates instances existing before the module load 
 
var foo = new Foo('foo'); 
 

 
// runs after user requests a module load, runs from the module 
 
setTimeout(function() { 
 
    // adding a module specific property to the non-module model 
 
    addProperty(Foo, 'getVal'); 
 

 
    console.log(foo.getVal()); 
 
    console.log(foo.getVal.getType()); 
 

 
    // simulates instances created after the module load 
 
    var bar = new Foo(17); 
 
    console.log(bar.getVal.getType()); // called before thunk - fails 
 
    console.log(bar.getVal()); 
 
    console.log(bar.getVal.getType()); // called after thunk - works 
 
}, 0);

一つの問題は、プロパティの値そのものは、アプリケーションコードは時々上記f.getTypeと同様に、プロパティ自体を呼び出さずに参照するプロパティを持っているということです。最初にfoo.getVal()を呼び出すことなく、foo.getVal.getType()のようなコールを適切に捕捉/転送/転送するにはどうすればよいですか?添付されたプロパティの名前はよく定義されていて共有できますが、正しいthisまたはそのデータにアクセスする方法はわかりません。

+0

これらの関数は怠惰サンクする必要があるだろう、なぜ私はあなたの理由を理解していないが、私はまた、これはgetType' 'に関係しているかを理解していません。方法が怠け者でなくても、その "方法の方法"はうまくいかないでしょう。 – Bergi

+0

この例ではそうではありません。アプリケーションでは、ユーザーがモジュールをロードしてネットワークコールが完了した後で追加されます。 'getType'メソッドは、プロパティを生成するライブラリからソートやその他の機能を提供するメソッドをシミュレートします。 – Iiridayn

+0

サンクは実際の実装を提供するモジュールをロードすると言っていますか?おもしろい... – Bergi

答えて

1

foo.getVal.getType()などのコールを適切にキャッチするにはどうすればよいですか。 foo.getVal()が最初に呼び出されなかった場合、私はこの正しいデータまたはそれらのデータにアクセスする方法を見ることができません。

以前と同じ方法を使用できますが、メソッド呼び出しレベルではなくゲッターレベルプロパティを使用できます。これが遅延し、任意のインスタンスのプロパティを作成することを可能にする:

function addProperty(Base, prop) { 
    Object.defineProperty(Base.prototype, prop, { 
    get: function thunk() { 
     var propertyValue = buildWorkFunction(typeof this.val); 
     //         ^^^^^^^^^^^^^^^ access the instance here 
     // overwrite (shadow) the prototype getter: 
     Object.defineProperty(this, prop, { 
     value: propertyValue, 
     writable: true, 
     enumerable: true, 
     configurable: true 
     }); 
     return this[prop]; 
    }, 
    enumerable: true, // dunno 
    configurable: true 
    }); 
} 
関連する問題