Dynamically add data to a javascript map

前端 未结 2 2060
情话喂你
情话喂你 2020-12-13 11:53

Is there a way I can dynamically add data to a map in javascript. A map.put(key,value)? I am using the yui libraries for javascript, but didn\'t see anything th

2条回答
  •  轮回少年
    2020-12-13 12:34

    Well any Javascript object functions sort-of like a "map"

    randomObject['hello'] = 'world';
    

    Typically people build simple objects for the purpose:

    var myMap = {};
    
    // ...
    
    myMap[newKey] = newValue;
    

    edit — well the problem with having an explicit "put" function is that you'd then have to go to pains to avoid having the function itself look like part of the map. It's not really a Javascripty thing to do.

    13 Feb 2014 — modern JavaScript has facilities for creating object properties that aren't enumerable, and it's pretty easy to do. However, it's still the case that a "put" property, enumerable or not, would claim the property name "put" and make it unavailable. That is, there's still only one namespace per object.

提交回复
热议问题