Small doc fixes.
[mootools.git] / Docs / Types / Object.md
blob67acdcfdca6d0d274d0f04446b10b6ad5168eff2
1 Type: Object {#Object}
2 ======================
4 A collection of Object functions.
6 ### See Also:
8 - [MDC Object][]
10 Function: Object.each {#Object:Object-each}
11 ------------------------------------
13 Used to iterate through an object.
15 ### Syntax:
17         Object.each(obj, fn[, bind]);
19 ### Arguments:
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][].
25 #### Argument: fn
27 ##### Syntax:
29         fn(item, index, object)
31 ##### Arguments:
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.
37 ### Example:
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);
42         });
44 ### Notes:
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.
55 ### Syntax:
57         var merged = Object.merge(obj1, obj2[, obj3[, ...]]);
59 ### Arguments:
61 1. (objects) Any number of objects.
63 ### Returns:
65 * (*object*) The object that is created as a result of merging all the objects passed in.
67 ### Examples:
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.
86 ### Syntax:
88         var clone = Object.clone(obj);
90 ### Arguments:
92 1. (obj) The object to clone
94 ### Returns:
96 * (*object*) A copy of the passed object
98 ### Example:
100         var obj1 = {a: 0, b: 1};
101         var obj2 = Object.clone(obj1);
103         obj1.a = 42;
104         alert(obj1.a);  // alerts '42'
105         alert(obj2.a);  // alerts '0'
107 ### Notes:
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.
118 ### Syntax:
120         Object.append(original, extension);
122 ### Arguments:
124 1. original  - (*object*) The object to be extended.
125 2. extension - (*object*) The object whose properties will be copied to original.
127 ### Returns:
129 * (*object*) The first object passed in, extended.
131 ### Examples:
133         var firstObj = {
134                 name: 'John',
135                 lastName: 'Doe'
136         };
137         var secondObj = {
138                 age: '20',
139                 sex: 'male',
140                 lastName: 'Dorian'
141         };
142         Object.append(firstObj, secondObj);
143         //firstObj is now: {name: 'John', lastName: 'Dorian', age: '20', sex: 'male'};
145 ### Notes:
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.
155 ### Syntax:
157         Object.subset(object, keys);
159 ### Arguments:
161 1. object - (*object*) The object.
162 2. keys - (*array*) An array with the keys.
164 ### Returns:
166 * (*object*) The subset of the Object.
168 ### Examples:
170         var object = {
171                 a: 'one',
172                 b: 'two',
173                 c: 'three'
174         };
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.
184 ### Syntax:
186         var mappedObject = Object.map(object, fn[, bind]);
188 ### Arguments:
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][].
194 #### Argument: fn
196 ##### Syntax:
198         fn(value, key, object)
200 ##### Arguments:
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.
206 ### Returns:
208 * (*object*) The new mapped object.
210 ### Examples:
212         var myObject = {a: 1, b: 2, c: 3};
213         var timesTwo = Object.map(timesTwo, function(value, key){
214                 return value * 2;
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.
224 ### Syntax:
226         var filteredObject = Object.filter(object, fn[, bind]);
228 ### Arguments:
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][].
234 #### Argument: fn
236 ##### Syntax:
238         fn(value, key, object)
240 ##### Arguments:
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.
246 ### Returns:
248 * (*object*) The new filtered object.
250 ### Examples:
252         var myObject = {a: 10, b: 20, c: 30};
253         var biggerThanTwenty = Object.filter(myObject, function(value, key){
254                 return value > 20;
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.
264 ### Syntax:
266         var allPassed = Object.every(object, fn[, bind]);
268 ### Arguments:
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][].
274 #### Argument: fn
276 ##### Syntax:
278         fn(value, key, object)
280 ##### Arguments:
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.
286 ### Returns:
288 * (*boolean*) If every value in the Object satisfies the provided testing function, returns true. Otherwise, returns false.
290 ### Examples:
292         var myObject = {a: 10, b: 4, c: 25, d: 100};
293         var areAllBigEnough = Object.every(myObject, function(value, key){
294                 return value > 20;
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.
304 ### Syntax:
306         var anyPassed = Object.some(object, fn[, bind]);
308 ### Arguments:
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][].
314 #### Argument: fn
316 ##### Syntax:
318         fn(value, key, object)
321 ##### Arguments:
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.
327 ### Returns:
329 * (*boolean*) If any value in the object satisfies the provided testing function, returns true. Otherwise, returns false.
331 ### Examples:
333         var myObject = {a: 10, b: 4, c: 25, d: 100};
334         var areAnyBigEnough = Object.some(myObject, function(value, key){
335                 return value > 20;
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][].
345 ### Syntax:
347         var keys = Object.keys(object);
349 ### Arguments:
351 1. object - (*object*) The object.
353 ### Returns:
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][].
364 ### Syntax:
366         var values = Object.values(object);
368 ### Arguments:
370 1. object - (*object*) The object.
372 ### Returns:
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.
383 ### Syntax:
385         var length = Object.getLength(object);
387 ### Arguments:
389 1. object - (*object*) The object.
391 ### Returns:
393 * (*number*) The length of the object.
395 ### Examples:
397         var myObject = {
398                 name: 'John',
399                 lastName: 'Doe'
400         });
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][].
410 ### Syntax:
412         var key = Object.keyOf(object);
414 ### Arguments:
416 1. object - (*object*) The object.
417 2. item - (*mixed*) The item to search for in the object.
419 ### Returns:
421 * (*string*) If the object has a the specified item in it, returns the key of that item.
422 * (*boolean*) Otherwise, returns false.
424 ### Examples:
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.
438 ### Syntax:
440         var inObject = Object.contains(object, value);
442 ### Arguments:
444 1. object - (*object*) The object.
445 2. value - (*mixed*) The value to search for in the Object.
447 ### Returns:
449 * (*boolean*) If the object has the passed in value in any of the keys, returns true. Otherwise, returns false.
451 ### Examples:
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.
464 ### Syntax:
466         var queryString = Object.toQueryString(object);
468 ### Arguments:
470 1. object - (*object*) The object.
471 2. source - (*object*) The object to generate the query string from.
473 ### Returns:
475 * (*string*) The query string.
477 ### Examples:
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 -------------------------------------------------
527 Use `myObject.myKey`
530 Hash Method: set {#Deprecated-Functions:Hash:set}
531 -------------------------------------------------
533 Use `myObject.myKey = value`
536 Hash Method: empty {#Deprecated-Functions:Hash:empty}
537 -----------------------------------------------------
539 Use `myObject = {}`
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
593 [Object]: #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