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

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

问题描述

有没有办法可以在javascript中动态地将数据添加到地图中。 map.put(键,值)?我使用yui库为javascript,但没有看到任何支持这个。

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;

edit —具有明确的放置功能的问题在于,您必须努力避免使功能本身看起来像地图的一部分。这不是一个真正的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并使其不可用。也就是说,每个对象仍然只有一个命名空间。

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天全站免登陆