In JavaScript, every object is a dictionary (an associative array). Your example can be written as an object literal:
var parameters = {
link: "http://mysite.com",
name: "This is an test"
};
// Read access
console.log(parameters.link); // or:
console.log(parameters["link"]);
// Write access
parameters.link = "http://yoursite.com"; // or:
parameters["link"] = "http://yoursite.com";
parameters.status = "OK"; // or:
parameters["status"] = "OK";
This example shows that the values (field values) of the dictionary (object) are usually accessed by writing the key (field name) using dot notation, but you can also use the index notation, as in case of C# dictionaries. Note that JavaScript is a dynamic language, so the type of keys and values can be anything. So you can define a dictionary (object) like this one:
var parameters = {
link: "http://mysite.com",
name: "This is an test",
id: 3,
4: "$#!+",
5: 123456
};
The values (field values) can be other objects or functions, too. If a value is a function, it is called a method of the JavaScript object.
=== UPDATE ===
Here is an approximation of the C# Dictionary<TKey,TValue> class in JavaScript (the test page is here). This code works, but I strongly recommend doing things in JavaScript the JavaScript way: objects can be used directly as associative arrays, there is no real need to use such a wrapper!
var Dictionary = (function () {
function Dictionary() {
if (!(this instanceof Dictionary))
return new Dictionary();
}
Dictionary.prototype.Count = function() {
var key,
count = 0;
for (key in this) {
if (this.hasOwnProperty(key))
count += 1;
}
return count;
};
Dictionary.prototype.Keys = function() {
var key,
keys = [];
for (key in this) {
if (this.hasOwnProperty(key))
keys.push(key);
}
return keys;
};
Dictionary.prototype.Values = function() {
var key,
values = [];
for (key in this) {
if (this.hasOwnProperty(key))
values.push(this[key]);
}
return values;
};
Dictionary.prototype.KeyValuePairs = function() {
var key,
keyValuePairs = [];
for (key in this) {
if (this.hasOwnProperty(key))
keyValuePairs.push({
Key: key,
Value: this[key]
});
}
return keyValuePairs;
};
Dictionary.prototype.Add = function(key, value) {
this[key] = value;
}
Dictionary.prototype.Clear = function() {
var key,
dummy;
for (key in this) {
if (this.hasOwnProperty(key))
dummy = delete this[key];
}
}
Dictionary.prototype.ContainsKey = function(key) {
return this.hasOwnProperty(key);
}
Dictionary.prototype.ContainsValue = function(value) {
var key;
for (key in this) {
if (this.hasOwnProperty(key) && this[key] === value)
return true;
}
return false;
}
Dictionary.prototype.Remove = function(key) {
var dummy;
if (this.hasOwnProperty(key)) {
dummy = delete this[key];
return true;
} else
return false;
}
return Dictionary;
}());
Test code:
var d = new Dictionary(),
i,
keyValuePair;
console.clear();
// Adding new elements.
d.Add("key1", "value1");
d.Add("key2", "value2");
// This also works. There is no KeyNotFoundException,
// the key-value pairs are inserted silently.
d["key3"] = "value3";
d["key4"] = "value4";
// Getting the number of key-value pairs.
console.log("Count:", d.Count());
// Getting an array containing the keys.
console.log("Keys:", d.Keys());
// Getting an array containing the values.
console.log("Values:", d.Values());
// Iterating over the key-value pairs.
for (i = 0; i < d.KeyValuePairs().length; i += 1) {
keyValuePair = d.KeyValuePairs()[i];
console.log("#", i + 1, keyValuePair.Key, "=>", keyValuePair.Value);
}
// Determining whether the dictionary contains a given key.
console.log("key2?", d.ContainsKey("key2"));
console.log("keyhole?", d.ContainsKey("keyhole"));
// Determining whether the dictionary contains a given value.
console.log("value2?", d.ContainsValue("value2"));
console.log("valuable?", d.ContainsValue("valuable"));
// Removing a value with a given key.
console.log("Removing key2:", d.Remove("key2"));
console.log("Count after Remove():", d.Count());
console.log("Removing keyhole:", d.Remove("keyhole"));
console.log("Count after Remove():", d.Count());
// Clearing all key-value pairs.
d.Clear();
console.log("Count after Clear():", d.Count());
Console output (press F12):
Count: 4
Keys: ["key1", "key2", "key3", "key4"]
Values: ["value1", "value2", "value3", "value4"]
# 1 key1 => value1
# 2 key2 => value2
# 3 key3 => value3
# 4 key4 => value4
key2? true
keyhole? false
value2? true
valuable? false
Removing key2: true
Count after Remove(): 3
Removing keyhole: false
Count after Remove(): 3
Count after Clear(): 0