0

tracedObj.squared(9)はなぜ返されませんか?ES6:ハーモニープロキシ:なぜ `tracedObj.squared(9)`は未定義に戻りますか?

これはおそらくobjの範囲は、それはそれ自身のオブジェクトのメソッドを呼び出した後、それはsquaredthis呼び出しのために間違った範囲内にあるとは何かを持っています。

コード

"use strict"; 
var Proxy = require('harmony-proxy'); 

function traceMethodCalls(obj) { 
    let handler = { 
     get(target, propKey, receiver) { 
      const origMethod = target[propKey]; 
      return function(...args) { 
       let result = origMethod.apply(this, args); 
       console.log(propKey + JSON.stringify(args) + ' -> ' + JSON.stringify(result)); 
      }; 
     } 
    }; 
    return new Proxy(obj, handler); 
} 

let obj = { 

    multiply(x, y) { 
     return x * y; 
    }, 
    squared(x) { 
     return this.multiply(x, x); 
    } 
}; 

let tracedObj = traceMethodCalls(obj); 
tracedObj.multiply(2,7); 

tracedObj.squared(9); 
obj.squared(9); 

出力

multiply[2,7] -> 14 
multiply[9,9] -> 81 
squared[9] -> undefined 
undefined 

Iは、ノードV4.4.3(それはあまりにもすぐにこれらを使用することです?)

実行コード

Iを使用していますこのようなコマンドを実行する必要があります:

node --harmony-proxies --harmony ./AOPTest.js

+0

が欠落している、ここで、このためのコードが見つかりました:http://www.2ality.com/2015/10/intercepting-method-calls.htmlを、私は著者を信じますFFを使用しています。 – leeand00

+1

あなたは決してあなたの交換関数で 'return result;'を返しませんか? – loganfsmyth

+0

@loganfsmythそれでした。 – leeand00

答えて

1
return function(...args) { 
    let result = origMethod.apply(this, args); 
    console.log(propKey + JSON.stringify(args) + ' -> ' + JSON.stringify(result)); 
}; 

return result; 
関連する問題