创建像Javascript的字典对象。净字典、对象、Javascript

2023-09-04 00:52:19 作者:青春如此疯狂℡

我想使用JavaScript创建一个对象,将存储在键,值对的价值观和我应该能够通过一些关键的,应该能够得到它的价值了。在.NET世界中,我们可以用词典类这种实现。我们有在JavaScript世界上的任何选项?我使用ExtJS的4.1,所以如果你知道的ExtJS的任何选项,甚至会工作。

我曾尝试这样的事情,但者皆我不能得到的价值。的

  VAR的widget =功能(K,V){
    this.key = K;
    v THIS.VALUE =;
};

VAR部件= [
    新的小工具(35,312),
    新的窗口小部件(52,32)
]。
 

解决方案

只需使用一个标准的JavaScript对象:

  VAR字典= {}; //创建新对象
词典[键1] =值1; //设置键1
VAR键1 =辞典[键1]; //获取KEY1
 

请注意:您还可以得到/设置任何钥匙您创建使用点符号(即 dictionary.key1

关于Node.js开发的的5个原因

您可以进一步考虑,如果你想具体功能吧...

 功能词典(){
   VAR词典= {};

   this.setData =功能(键,VAL){词典[关键] = VAL; }
   this.getData =功能(键){返回词典[关键]; }
}

VAR词典=新词典();
dictionary.setData(键1,值1);
VAR键1 = dictionary.getData(键1);
 

I want to create a object in JavaScript which will store values in key, value pair and I should be able to pass some key and should be able to get its value back. In .NET world we can use dictionary class for this kind of implementation. Do we have any option in JavaScript world? I am using ExtJs 4.1, so if you know of any option in ExtJS even that would work.

I have tried something like this but I cannot get value by key.

var Widget = function(k, v) {
    this.key = k;
    this.value = v;
};

var widgets = [
    new Widget(35, 312),
    new Widget(52, 32)
];

解决方案

Just use a standard javascript object:

var dictionary = {};//create new object
dictionary["key1"] = value1;//set key1
var key1 = dictionary["key1"];//get key1

NOTE: You can also get/set any "keys" you create using dot notation (i.e. dictionary.key1)

You could take that further if you wanted specific functions for it...

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");