
A custom Object ({}) implementation which does not account for prototypes when setting, getting, or iterating. Useful because in JavaScript, we cannot use Object.prototype. Instead, we can use Hash.prototype!
var myHash = new Hash([object]);
var myHash = new Hash({ aProperty: true, aMethod: function(){ return true; } }); alert(myHash.has('aMethod')); //Returns true.
Calls a function for each key-value pair in the object.
myHash.each(fn[, bind]);
fn(value, key, hash)
var hash = new Hash({first: "Sunday", second: "Monday", third: "Tuesday"}); hash.each(function(value, key){ alert("the " + key + " day of the week is " + value); }); //Alerts "the first day of the week is Sunday", "the second day of the week is Monday", etc.
Tests for the presence of a specified key in the Hash.
var inHash = myHash.has(item);
var hash = new Hash({'a': 'one', 'b': 'two', 'c': 'three'}); hash.has('a'); //returns true hash.has('d'); //returns false
Returns the key of the specified value. Synonymous with Array:indexOf.
var key = myHash.keyOf(item);
var hash = new Hash({'a': 'one', 'b': 'two', 'c': 3}); hash.keyOf('two'); //returns 'b' hash.keyOf(3); //returns 'c' hash.keyOf('four') //returns false
Tests for the presence of a specified value in the Hash.
var inHash = myHash.hasValue(value);
var hash = new Hash({'a': 'one', 'b': 'two', 'c': 'three'}); hash.hasValue('one'); //returns true hash.hasValue('four'); //returns false
Extends this Hash with the key-value pairs from the object passed in.
myHash.extend(properties);
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); var properties = { 'age': '20', 'sex': 'male', 'lastName': 'Dorian' }; hash.extend(properties); //hash now holds an object containing: { 'name': 'John', 'lastName': 'Dorian', 'age': '20', 'sex': 'male' };
Combines this Hash with the key-value pairs of the object passed in. Does not allow duplicates (old values are not overwritten by new ones) and is case and type sensitive.
myHash.combine(properties);
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); var properties = { 'name': 'Jane' 'age': '20', 'sex': 'male', 'lastName': 'Dorian' }; hash.combine(properties); //hash now holds an object containing: { 'name': 'John', 'lastName': 'Doe', 'age': '20', 'sex': 'male' };
Removes the specified key from the Hash.
myHash.erase(key);
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash.erase('lastName'); //hash now holds an object containing: { 'name': 'John' };
Retrieves a value from the hash.
myHash.get(key);
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash.get('name'); //returns 'John'
Adds a key-value pair to the hash or replaces a previous value associated with the specified key.
myHash.set(key, value);
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash.set('name', 'Michelle'); //hash.name is now 'Michelle'
Empties the hash.
myHash.empty();
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash.empty(); //hash now holds an empty object: {}
Includes the specified key-value pair in the Hash if the key doesn't already exist.
myHash.include(key, value);
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash.include('name', 'Michelle'); //hash is unchanged hash.include('age', 25); //hash.age is now 25
Creates a new map with the results of calling a provided function on every value in the map.
var mappedHash = myHash.map(fn[, bind]);
fn(value, key, hash)
var timesTwo = new Hash({a: 1, b: 2, c: 3}).map(function(value, key){ return value * 2; }); //timesTwo now holds an object containing: {a: 2, b: 4, c: 6};
Creates a new Hash with all of the elements of the Hash for which the provided filtering function returns true.
var filteredHash = myHash.filter(fn[, bind]);
fn(value, key, hash)
var biggerThanTwenty = new Hash({a: 10, b: 20, c: 30}).filter(function(value, key){ return value > 20; }); //biggerThanTwenty now holds an object containing: {c: 30}
Returns true if every value in the object satisfies the provided testing function.
var allPassed = myHash.every(fn[, bind]);
fn(value, key, hash)
var areAllBigEnough = ({a: 10, b: 4, c: 25, d: 100}).every(function(value, key){ return value > 20; }); //areAllBigEnough = false
Returns true if at least one value in the object satisfies the provided testing function.
var anyPassed = myHash.any(fn[, bind]);
fn(value, key, hash)
var areAnyBigEnough = ({a: 10, b: 4, c: 25, d: 100}).some(function(value, key){ return value > 20; }); //isAnyBigEnough = true
Returns a a clean object from an Hash.
myHash.getClean();
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash = hash.getClean(); // hash doesnt contain Hash prototypes anymore hash.each() //error!
Returns an array containing all the keys, in the same order as the values returned by Hash:getValues.
var keys = myHash.getKeys();
Returns an array containing all the values, in the same order as the keys returned by Hash:getKeys.
var values = myHash.getValues();
Returns the number of keys in the Hash.
var length = myHash.getLength();
var hash = new Hash({ 'name': 'John', 'lastName': 'Doe' }); hash.getLength(); // returns 2
Generates a query string from key/value pairs in an object and URI encodes the values.
var queryString = myHash.toQueryString();
Hash.toQueryString({apple: "red", lemon: "yellow"}); //returns "apple=red&lemon=yellow"
var myHash = new Hash({apple: "red", lemon: "yellow"}); myHash.toQueryString(); //returns "apple=red&lemon=yellow"
Shortcut for the new Hash.
© Linux.ria.ua, 2008-2024 |