4 A collection of Object functions.
10 Function: Object.each {#Object:Object-each}
11 ------------------------------------
13 Used to iterate through an object.
17 Object.each(obj, fn[, bind]);
21 1. obj - (*object*) The object to iterate through.
22 2. fn - (*function*) The function to test for each element.
23 3. bind - (*object*, optional) The object to use as 'this' within the function. For more information see [Function:bind][].
29 fn(item, index, object)
33 1. item - (*mixed*) The current item in the array.
34 2. index - (*number*) The current item's key.
35 3. object - (*mixed*) The actual array/object.
39 // alerts 'The first day of the week is Sunday', 'The second day of the week is Monday', etc.:
40 Object.each({first: 'Sunday', second: 'Monday', third: 'Tuesday'}, function(value, key){
41 alert('The ' + key + ' day of the week is ' + value);
46 This method is an object-specific equivalent of *$each* from MooTools 1.2.
50 Function: Object.merge {#Object:Object-merge}
51 --------------------------------------
53 Merges any number of objects recursively without referencing them or their sub-objects.
57 var merged = Object.merge(obj1, obj2[, obj3[, ...]]);
61 1. (objects) Any number of objects.
65 * (*object*) The object that is created as a result of merging all the objects passed in.
69 var obj1 = {a: 0, b: 1};
70 var obj2 = {c: 2, d: 3};
71 var obj3 = {a: 4, d: 5};
72 var merged = Object.merge(obj1, obj2, obj3); // returns {a: 4, b: 1, c: 2, d: 5}, (obj2, and obj3 are unaltered)
74 merged === obj1; // true, obj1 gets altered and returned as merged object
76 var nestedObj1 = {a: {b: 1, c: 1}};
77 var nestedObj2 = {a: {b: 2}};
78 var nested = Object.merge(nestedObj1, nestedObj2); // returns: {a: {b: 2, c: 1}}
81 Function: Object.clone {#Object:Object-clone}
82 --------------------------------------
84 Returns a copy of an object.
88 var clone = Object.clone(obj);
92 1. (obj) The object to clone
96 * (*object*) A copy of the passed object
100 var obj1 = {a: 0, b: 1};
101 var obj2 = Object.clone(obj1);
104 alert(obj1.a); // alerts '42'
105 alert(obj2.a); // alerts '0'
109 This is an object-specific equivalent of *$unlink* from MooTools 1.2.
113 Function: Object.append {#Object:Object-append}
114 ----------------------------------------
116 Copies all the properties from the second object passed in to the first object passed in.
120 Object.append(original, extension);
124 1. original - (*object*) The object to be extended.
125 2. extension - (*object*) The object whose properties will be copied to original.
129 * (*object*) The first object passed in, extended.
142 Object.append(firstObj, secondObj);
143 //firstObj is now: {name: 'John', lastName: 'Dorian', age: '20', sex: 'male'};
147 This method is an object-specific equivalent of *$extend* from MooTools 1.2.
150 Function: Object.subset {#Object:Object-subset}
151 ----------------------------------------
153 Get a subset of an object.
157 Object.subset(object, keys);
161 1. object - (*object*) The object.
162 2. keys - (*array*) An array with the keys.
166 * (*object*) The subset of the Object.
175 Object.subset(object, ['a', 'c']); // returns {a: 'one', c: 'three'}
179 Function: Object.map {#Object:Object-map}
180 ----------------------------
182 Creates a new map with the results of calling a provided function on every value in the map.
186 var mappedObject = Object.map(object, fn[, bind]);
190 1. object - (*object*) The object.
191 2. fn - (*function*) The function to produce an element of the Object from an element of the current one.
192 3. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
198 fn(value, key, object)
202 1. value - (*mixed*) The current value in the object.
203 2. key - (*string*) The current value's key in the object.
204 3. object - (*object*) The actual object.
208 * (*object*) The new mapped object.
212 var myObject = {a: 1, b: 2, c: 3};
213 var timesTwo = Object.map(timesTwo, function(value, key){
215 }); // timesTwo now holds an object containing: {a: 2, b: 4, c: 6};
219 Function: Object.filter {#Object:Object-filter}
220 ----------------------------------
222 Creates a new object with all of the elements of the object for which the provided filtering function returns true.
226 var filteredObject = Object.filter(object, fn[, bind]);
230 1. object - (*object*) The object.
231 2. fn - (*function*) The function to test each element of the Object. This function is passed the value and its key in the Object.
232 3. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
238 fn(value, key, object)
242 1. value - (*mixed*) The current value in the object.
243 2. key - (*string*) The current value's key in the object.
244 3. object - (*object*) The actual object.
248 * (*object*) The new filtered object.
252 var myObject = {a: 10, b: 20, c: 30};
253 var biggerThanTwenty = Object.filter(myObject, function(value, key){
255 }); // biggerThanTwenty now holds an object containing: {c: 30}
259 Function: Object.every {#Object:Object-every}
260 --------------------------------
262 Returns true if every value in the object satisfies the provided testing function.
266 var allPassed = Object.every(object, fn[, bind]);
270 1. object - (*object*) The object.
271 2. fn - (*function*) The function to test each element of the Object. This function is passed the value and its key in the Object.
272 3. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
278 fn(value, key, object)
282 1. value - (*mixed*) The current value in the object.
283 2. key - (*string*) The current value's key in the object.
284 3. object - (*object*) The actual object.
288 * (*boolean*) If every value in the Object satisfies the provided testing function, returns true. Otherwise, returns false.
292 var myObject = {a: 10, b: 4, c: 25, d: 100};
293 var areAllBigEnough = Object.every(myObject, function(value, key){
295 }); // areAllBigEnough = false
299 Function: Object.some {#Object:Object-some}
300 ------------------------------
302 Returns true if at least one value in the object satisfies the provided testing function.
306 var anyPassed = Object.some(object, fn[, bind]);
310 1. object - (*object*) The object.
311 2. fn - (*function*) The function to test each element of the object. This function is passed the value and its key in the object.
312 3. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
318 fn(value, key, object)
323 1. value - (*mixed*) The current value in the object.
324 2. key - (*string*) The current value's key in the object.
325 3. object - (*object*) The actual object.
329 * (*boolean*) If any value in the object satisfies the provided testing function, returns true. Otherwise, returns false.
333 var myObject = {a: 10, b: 4, c: 25, d: 100};
334 var areAnyBigEnough = Object.some(myObject, function(value, key){
336 }); //isAnyBigEnough = true
340 Function: Object.keys {#Object:Object-keys}
341 ------------------------------------
343 Returns an array containing all the keys, in the same order as the values returned by [Object:values][].
347 var keys = Object.keys(object);
351 1. object - (*object*) The object.
355 * (*array*) An array containing all the keys of the object.
359 Function: Object.values {#Object:Object-values}
360 ----------------------------------------
362 Returns an array containing all the values, in the same order as the keys returned by [Object:keys][].
366 var values = Object.values(object);
370 1. object - (*object*) The object.
374 * (*array*) An array containing all the values of the object.
378 Function: Object.getLength {#Object:Object-getLength}
379 ----------------------------------------
381 Returns the number of keys in the object.
385 var length = Object.getLength(object);
389 1. object - (*object*) The object.
393 * (*number*) The length of the object.
401 Object.getLength(myObject); // returns 2
405 Function: Object.keyOf {#Object:Object-keyOf}
406 --------------------------------
408 Returns the key of the specified value. Synonymous with [Array:indexOf][].
412 var key = Object.keyOf(object);
416 1. object - (*object*) The object.
417 2. item - (*mixed*) The item to search for in the object.
421 * (*string*) If the object has a the specified item in it, returns the key of that item.
422 * (*boolean*) Otherwise, returns false.
426 var myObject = {a: 'one', b: 'two', c: 3};
427 Object.keyOf(myObject,'two'); // returns 'b'
428 Object.keyOf(myObject,3); // returns 'c'
429 Object.keyOf(myObject,'four'); // returns false
433 Function: Object.contains {#Object:Object-contains}
434 --------------------------------------
436 Tests for the presence of a specified value in the object.
440 var inObject = Object.contains(object, value);
444 1. object - (*object*) The object.
445 2. value - (*mixed*) The value to search for in the Object.
449 * (*boolean*) If the object has the passed in value in any of the keys, returns true. Otherwise, returns false.
453 var myObject = {a: 'one', b: 'two', c: 'three'};
454 Object.contains(myObject, 'one'); // returns true
455 Object.contains(myObject, 'four'); // returns false
459 Function: Object.toQueryString {#Object:Object-toQueryString}
460 ------------------------------------------------
462 Generates a query string from key/value pairs in an object and URI encodes the values.
466 var queryString = Object.toQueryString(object);
470 1. object - (*object*) The object.
471 2. source - (*object*) The object to generate the query string from.
475 * (*string*) The query string.
479 Object.toQueryString({apple: 'red', lemon: 'yellow'}); // returns 'apple=red&lemon=yellow'
482 Deprecated Functions {#Deprecated-Functions}
483 ============================================
485 Type: Hash {#Deprecated-Functions:Hash}
486 --------------------------------------
488 Hash has been deprecated. Each Hash method has a similar Object method or a Vanilla JS equivalent.
490 Hash Method: has {#Deprecated-Functions:Hash:has}
491 -------------------------------------------------
493 You could simply use `myObject.myKey != undefined`
495 Hash Method: keyOf {#Deprecated-Functions:Hash:keyOf}
496 -----------------------------------------------------
498 Use [Object.keyOf](#Object:Object-keyOf)
501 Hash Method: hasValue {#Deprecated-Functions:Hash:hasValue}
502 ------------------------------------------------------------
504 Use [Object.contains](#Object:Object-contains)
507 Hash Method: extend {#Deprecated-Functions:Hash:extend}
508 --------------------------------------------------------
510 Use [Object.append](#Object:Object-append)
513 Hash Method: combine {#Deprecated-Functions:Hash:combine}
514 ---------------------------------------------------------
516 Use [Object.merge](#Object:Object-merge)
519 Hash Method: erase {#Deprecated-Functions:Hash:erase}
520 -----------------------------------------------------
522 Use `delete myObject.a`
524 Hash Method: get {#Deprecated-Functions:Hash:get}
525 -------------------------------------------------
530 Hash Method: set {#Deprecated-Functions:Hash:set}
531 -------------------------------------------------
533 Use `myObject.myKey = value`
536 Hash Method: empty {#Deprecated-Functions:Hash:empty}
537 -----------------------------------------------------
542 Hash Method: include {#Deprecated-Functions:Hash:include}
543 ---------------------------------------------------------
545 Use `if(myObject.myKey == undefined) myObject.myKey = value`
548 Hash Method: map {#Deprecated-Functions:Hash:map}
549 -------------------------------------------------
551 Use [Object.map](#Object:Object-map)
554 Hash Method: filter {#Deprecated-Functions:Hash:filter}
555 -------------------------------------------------------
557 Use [Object.filter](#Object:Object-filter)
560 Hash Method: every {#Deprecated-Functions:Hash:every}
561 -----------------------------------------------------
563 Use [Object.every](#Object:Object-every)
566 Hash Method: some {#Deprecated-Functions:Hash:some}
567 -------------------------------------------------------
569 Use [Object.some](#Object:Object-some)
572 Hash Method: getKeys {#Deprecated-Functions:Hash:getKeys}
573 -------------------------------------------------------
575 Use [Object.keys](#Object:Object-keys)
578 Hash Method: getValues {#Deprecated-Functions:Hash:getValues}
579 ------------------------------------------------------------
581 Use [Object.values](#Object:Object-values)
584 Hash Method: toQueryString {#Deprecated-Functions:Hash:toQueryString}
585 -------------------------------------------------------
587 Use [Object.toQueryString](#Object:Object-toQueryString)
592 [MDC Object]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object
594 [Array:indexOf]: /core/Types/Array/#Array:indexOf
595 [Object:values]: #Object:values
596 [Object:keys]: #Object:keys
597 [Function:bind]: /core/Types/Function#Function:bind