我動態建立的對象將具有在其上的發送方:類型錯誤:此[(( 「_」 +(中間值))+ 「_listener」)]不是一個函數
var coins = {};
['USD','EUR'].forEach((product_id) => {
coins[`_${product_id}`] = {};
coins[`_${product_id}_listener`] = (val) => {
log.info(process.pid, 'terminal send', product_id, [`_${product_id}`]);
terminal.send({
action: product_id,
data: this[`_${product_id}`],
timestamp: new Date,
});
};
Object.defineProperty(coins, product_id, {
set: (val) => {
this[`_${product_id}`] = val;
this[`_${product_id}_listener`](val);
},
get: (val) => {
return this[`_${product_id}`];
},
});
});
不幸的是,當我設置一個硬幣的東西......
coins['USD'] = {a: 4};
我得到一個錯誤:
TypeError: this[(("_" + (intermediate value)) + "_listener")] is not a function
at Object.set (repl:15:32)
at repl:1:18
at sigintHandlersWrap (vm.js:22:35)
at sigintHandlersWrap (vm.js:73:12)
at ContextifyScript.Script.runInThisContext (vm.js:21:12)
at REPLServer.defaultEval (repl.js:340:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:538:10)
at emitOne (events.js:101:20)
這顯然是指T的第二線他是二傳手,但我不知道爲什麼。如果我評論一下,得到並設置工作正常。我的聽衆只是不開火...顯然。這裏的目標是讓聽衆開火。
跟進
我也注意到,與該行註釋掉,該對象看起來不權(IMO):
> coins['USD'] = {a: 4};
{ a: 4 }
> coins
{ _USD: {},
_USD_listener: [Function],
_EUR: {},
_EUR_listener: [Function] }
> coins['_USD']
{}
> coins['USD']
{ a: 4 }
由於我的二傳手是supposively設置爲_USD
,爲什麼_USD
在我打印出coins
時看起來沒有設置?
非常感謝您的快速和完整的答案!起初,我認爲這是深夜編碼引起的問題,但現在我再次醒來,我仍然不確定你的第二個答案或[mozilla的例子](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set#Defining_a_setter_on_existing_objects_using_defineProperty)工作,而我的工作沒有。這個''['_ $ {product_id}'] = val'出現(在我身上)在'function'的上下文之外。我認爲這是一個特殊的對象指針,但我想有一些我並沒有完全掌握的繼承。 – VertigoRay
@VertigoRay考慮下面的例子'const obj = {fn(){return this},arrow:()=> this}'。如果你使用'console.log(obj.fn(),obj.arrow())',你會看到不同的輸出,因爲'obj.smth()'相當於'obj.smth.call(obj)'。如果'smth'是箭頭函數,它不會影響它的上下文,因爲它是一次性設置的,就像你使用普通函數「綁定」一樣。 –