用Javascript创建一个类似.net的字典对象
Creating a .net like dictionary object in Javascript
我想用JavaScript创建一个对象,它将在键、值对中存储值,我应该能够传递一些键,并且应该能够取回它的值。在.NET世界中,我们可以使用dictionary类来实现这种类型。在JavaScript世界里,我们有什么选择吗?我使用的是ExtJs 4.1,所以如果你知道ExtJs中的任何选项,即使是这样也可以。
我尝试过类似的方法,但无法通过键获取值
var Widget = function(k, v) {
this.key = k;
this.value = v;
};
var widgets = [
new Widget(35, 312),
new Widget(52, 32)
];
只需使用标准的javascript对象:
var dictionary = {};//create new object
dictionary["key1"] = value1;//set key1
var key1 = dictionary["key1"];//get key1
注意:您还可以获取/设置使用点符号(即dictionary.key1
)创建的任何"密钥"
如果你想要它的特定功能,你可以更进一步…
function Dictionary(){
var dictionary = {};
this.setData = function(key, val) { dictionary[key] = val; }
this.getData = function(key) { return dictionary[key]; }
}
var dictionary = new Dictionary();
dictionary.setData("key1", "value1");
var key1 = dictionary.getData("key1");
这门课取自Marijn Havereke的书《Eloquent JavaScript 》
小提琴
function Dictionary(values) {
this.values = values || {};
var forEachIn = function (object, action) {
for (var property in object) {
if (Object.prototype.hasOwnProperty.call(object, property))
action(property, object[property]);
}
};
Dictionary.prototype.containsKey = function(key) {
return Object.prototype.hasOwnProperty.call(this.values, key) &&
Object.prototype.propertyIsEnumerable.call(this.values, key);
};
Dictionary.prototype.forEach = function(action) {
forEachIn(this.values, action);
};
Dictionary.prototype.lookup = function(key) {
return this.values[key];
};
Dictionary.prototype.add = function(key, value) {
this.values[key] = value;
};
};
var numberDic = new Dictionary({One: 1,Two: 2, Three: 3});
//-- does key exist
console.log(numberDic.containsKey("One"));
console.log(numberDic.containsKey("One"));
console.log(numberDic.containsKey("Four"));
//-- loop through each item in the dic
numberDic.forEach(function(key, value) {
console.log(key, "is", value);
});
//-- works with complex objects
//------------------------------------------
var complexObjectDic = new Dictionary({
Microsoft: {
Something: "Real Interesting",
About: "Microsoft",
Will: "Go",
Here: ".",
ProductPrices: {
WindowsPhone: 55.88,
Windows :{
WinXp : 180.00,
Win7 : 200.00,
Win8 : 150.00
}
}
},
Apple: {
Did: "you",
Hear: "the",
New: "iphone",
Will: "be coming out soon",
}});
//-- does key exist
console.log(complexObjectDic.containsKey("Microsoft"));
console.log(complexObjectDic.containsKey("Apple"));
console.log(complexObjectDic.containsKey("Facebook"));
//-- search the dic by key
console.log(complexObjectDic.lookup("Microsoft"));
console.log(complexObjectDic.lookup("Apple"));
//-- add item to dic
complexObjectDic.add("Instagram", {
This: "is",
Another: "object",
That: "I willl be Adding"
});
//-- loop through each item in the dic
complexObjectDic.forEach(function(key, value) {
console.log(key, value);
});
var widget={};
var key='k';
widget[key]='v';
alert(widget.k);//gives you v
相关文章:
- ASP.net使对象可点击
- 如何从jQuery对象文本正确调用服务器端ASP.NET并将数据返回给jQuery
- 使用查询将对象数组发布到asp.net-mvc控制器操作的正确方法是什么
- 意外结果,在 ASP.Net 中解析 JSON 对象的数组
- 从目录中 json.net 对象
- Breeze.js/NET+EF复杂的对象行为相当奇怪
- 谷歌地图-来自VB.Net的JSON对象标记
- 如何从C#.Net Webbrowser对象访问javascript变量
- 使用javascript将JSON对象绑定到asp.net gridview控件
- jQuery:内容页面中预期的对象 ASP.NET
- asp.net 文字对象传递给 JavaScript
- 我们如何在 lift 中使用 net.liftweb.http.js.JsCmds.Script 对象
- 在 Telerik ASP.Net UI 客户端中,如果我以内联方式附加客户端事件,如何获取鼠标事件对象
- 使用具有有效 JavaScript 格式的 Json.NET 序列化对象
- ASP.NET 控制器中的 JSON 对象进行查看
- 如何在 vb.net 中将对象转换为数组
- 在视图中将.net对象转换为JSON对象
- 工具转换asp.net对象到javascript对象
- 如何处理Json.. NET对象引用
- 在WP7 WebBrowser控件中从Javascript调用.net对象