且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据动态添加到 javascript 地图

更新时间:2023-01-26 14:54:17

好吧,任何 Javascript 对象函数都有点像地图"

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;

编辑 —拥有显式放置"函数的问题在于,您必须竭尽全力避免让函数本身看起来像地图的一部分.这真的不是一个 Javascripty 的事情.

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.

2014 年 2 月 13 日现代 JavaScript 具有用于创建对象的工具不可枚举的属性,这很容易做到.但是,仍然存在put"属性,无论是否可枚举,都会声明属性名称为put"并使其不可用.也就是说,每个对象仍然只有一个命名空间.

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.