当密钥未知时直到运行时,并且当所有键都是相同类型且所有值都是相同类型时,请使用对象上的映射.
当存在对各个元素进行操作的逻辑时使用对象.
在对象上使用地图的适用示例是什么?特别是"什么时候直到运行时才能知道密钥?"
var myMap = new Map();
var keyObj = {},
keyFunc = function () { return 'hey'},
keyString = "a string";
// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");
console.log(myMap.get(keyFunc));
Run Code Online (Sandbox Code Playgroud)