将数据动态添加到 javascript 地图 [英] Dynamically add data to a javascript map

查看:23
本文介绍了将数据动态添加到 javascript 地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在 javascript 中动态地将数据添加到地图中.map.put(key,value)?我正在为 javascript 使用 yui 库,但没有看到任何支持它的内容.

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 there to support this.

推荐答案

好吧,任何 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.

这篇关于将数据动态添加到 javascript 地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆