我一直試圖讓ES2015地圖的概念,有一件事我不明白的保持如下:使用對象ES2015地圖鍵
var mapRawObj = new Map();
var rawObj = {j:"I like penguin"};
mapRawObj.set(rawObj,true);
console.log(mapRawObj.get(rawObj)); //this outputs true
mapRawObj.set({j:"I like polar bear"},true);
console.log(mapRawObj.get({j:"I like polar bear"})); //this outputs undefined
的第一個工程,第二個沒有按我不明白爲什麼?
我以爲當你把對象註冊爲鍵時,它就是對象本身,而不是對象的名字。這就是爲什麼在下面的例子中,當你重新分配密鑰對象時,它會失敗作爲密鑰?
具有相同數據var obj = { a:"hello ", b:"world "};
var mapObj = new Map();
mapObj.set(obj,true);
obj = {d:34}; //obj is re-assigned
console.log(mapObj.get(obj)); // outputs undefined
在第二個示例中,您基本上與第二個代碼段中的情況基本相同。所以要麼你瞭解這兩者,要麼都不瞭解。我很困惑。 – zeroflagL