7 ## bindAll(obj, [...methodNames]):void
9 Bind methods of the target object to always execute on its own context
10 (ovewritting the original function).
12 See: [function/bind](./function.html#bind)
17 logNameOnClick: function() {
18 console.log(this.name);
22 // binds all methods by default
24 jQuery('#docs').on('click', view.logNameOnClick);
27 You can also specify the list of methods that you want to bind (in case you
28 just want to bind a few of them).
31 // only the listed methods will be bound to `obj` context
32 bindAll(obj, 'logNameOnClick', 'doAwesomeStuffOnDrag');
37 ## contains(obj, value):Boolean
39 Similar to [Array/contains](array.html#contains). Checks if Object contains
48 contains(obj, 2); // true
49 contains(obj, 'foo'); // false
54 ## deepFillIn(target, ...objects):Object
56 Fill missing properties recursively.
58 It's different from `deepMixIn` since it won't override any existing property.
59 It's also different from `merge` since it won't clone child objects during the
62 It returns the target object and mutates it in place.
64 See: [`fillIn()`](#fillIn), [`deepMixIn()`](#deepMixIn), [`merge()`](#merge)
73 var options = deepFillIn({foo : { baz : 45 }, lorem : 'amet'}, base);
74 // > {foo: {bar:123, baz : 45}, lorem : 'amet'}
79 ## deepMatches(target, pattern):Boolean
81 Recursively checks if object contains all properties/value pairs. When both
82 the target and pattern values are arrays, it checks that the target value
83 contain matches for all the items in the pattern array (independent of order).
90 { type: 'cat', name: 'Grumpy Cat' },
91 { type: 'dog', name: 'Hawk' }
95 deepMatches(john, { name: 'John' }); // true
96 deepMatches(john, { age: 21 }); // false
97 deepMatches(john, { pets: [ { type: 'cat' } ] }); // true
98 deepMatches(john, { pets: [ { name: 'Hawk' } ] }); // true
99 deepMatches(john, { pets: [ { name: 'Hairball' } ] }); // false
102 See [`matches()`](#matches)
106 ## deepMixIn(target, ...objects):Object
108 Mixes objects into the target object, recursively mixing existing child objects
111 It will only recursively mix objects if both (existing and new) values are
114 Returns the target object. Like [`merge()`](#merge), but mutates the target
115 object, and does not clone child objects.
125 deepMixIn(target, { foo: { id: 2 } });
126 console.log(target); // { foo: { name: "foo", id: 2 } }
129 See: [`mixIn()`](#mixIn), [`merge()`](#merge), [`deepFillIn()`](#deepFillIn)
133 ## equals(a, b, [callback]):Boolean
135 Tests whether two objects contain the same keys and values.
137 `callback` specifies the equality comparison function used to compare the
138 values. It defaults to using [lang/is](lang.html#is).
140 It will only check the keys and values contained by the objects; it will not
141 check the objects' prototypes. If either of the values are not objects, they
142 will be compared using the `callback` function.
145 equals({}, {}); // true
146 equals({ a: 1 }, { a: 1 }); // true
147 equals({ a: 1 }, { a: 2 }); // false
148 equals({ a: 1, b: 2 }, { a: 1 }); // false
149 equals({ a: 1 }, { a: 1, b: 2 }); // false
150 equals(null, null); // true
151 equals(null, {}); // false
152 equals({ a: 1 }, { a: '1' }, function(a, b) { return a == b; }); // true
155 See: [array/equals](array.html#equals), [lang/deepEquals](lang.html#deepEquals)
158 ## every(obj, callback, [thisObj]):Boolean
160 Similar to [Array/every](array.html#every). Tests whether all properties in the
161 object pass the test implemented by the provided callback.
171 every(obj, isNumber); // false
176 ## fillIn(obj, ...default):Object
178 Fill in missing properties in object with values from the *defaults* objects.
185 fillIn({foo:'ipsum'}, base); // {foo:'ipsum', num:123}
187 PS: it allows merging multiple objects at once, the first ones will take
190 See: [`mixIn()`](#mixIn), [`merge()`](#merge), [`deepFillIn()`](#deepFillIn)
194 ## filter(obj, callback, [thisObj])
196 Returns a new object containing all properties where `callback` returns true,
197 similar to Array/filter. It does not use properties from the object's
200 Callback receives the same arguments as `forOwn()`.
202 See: [`forOwn()`](#forOwn), [`forIn()`](#forIn), [`pick()`](#pick)
210 // returns { bar: 'bar value' }
211 filter(obj, function(v) { return value.length > 5; });
213 // returns { foo: 'value' }
214 filter(obj, function(v, k) { return k === 'foo'; });
219 ## find(obj, callback, [thisObj])
221 Loops through all the properties in the Object and returns the first one that
222 passes a truth test (callback), similar to [Array/find](array.html#find).
223 Unlike Array/find, order of iteration is not guaranteed.
231 find(obj, isString); // 'foo'
232 find(obj, isNumber); // 12
237 ## forIn(obj, callback[, thisObj])
239 Iterate over all properties of an Object, similar to
240 [Array/forEach](array.html#forEach).
242 It [avoids don't enum bug on IE](https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug).
243 It **will** iterate over inherited (enumerable) properties from the prototype.
245 It allows exiting the iteration early by returning `false` on the callback.
247 See: [`forOwn()`](#forOwn), [`keys()`](#keys), [`values()`](#values)
249 ### Callback arguments
251 Callback will receive the following arguments:
253 1. Property Value (*)
255 3. Target object (Object)
265 Foo.prototype.lorem = 4;
272 forIn(obj, function(val, key, o){
277 console.log(result); // 7
278 console.log(keys); // ['foo', 'bar', 'lorem']
283 ## forOwn(obj, callback[, thisObj])
285 Iterate over all own properties from an Object, similar to
286 [Array/forEach](array.html#forEach).
288 It [avoids don't enum bug on IE](https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug).
289 Notice that it **won't** iterate over properties from the prototype.
291 It allows exiting the iteration early by returning `false` on the callback.
293 See: [`forIn()`](#forIn), [`keys()`](#keys), [`values()`](#values)
295 ### Callback arguments
297 Callback will receive the following arguments:
299 1. Property Value (*)
301 3. Target object (Object)
312 Foo.prototype.lorem = 4;
319 forOwn(obj, function(val, key, o){
324 console.log(result); // 3
325 console.log(keys); // ['foo', 'bar']
330 ## functions(obj):Array
332 Returns a sorted list of all enumerable properties that have function values
333 (including inherited properties).
340 functions(obj); // ['foo']
345 ## get(obj, propName):*
347 Returns nested property value. Will return `undefined` if property doesn't
350 See: [`set()`](#set), [`namespace()`](#namespace), [`has()`](#has)
361 get(lorem, 'ipsum.dolor.sit'); // "amet"
362 get(lorem, 'foo.bar'); // undefined
367 ## has(obj, propName):Boolean
369 Checks if object contains a child property. Useful for cases where you need to
370 check if an object contain a *nested* property. It will get properties
371 inherited by the prototype.
373 see: [`hasOwn()`](#hasOwn), [`get()`](#get)
382 has(a, 'b.c'); // true
383 has(a, 'foo.c'); // false
389 if( has(a, 'foo.c') ){ // false
393 if( a.foo.c ){ // ReferenceError: `foo` is not defined
400 ## hasOwn(obj, propName):Boolean
402 Safer `Object.hasOwnProperty`. Returns a boolean indicating whether the object
403 has the specified property.
410 hasOwnProperty : 'bar'
413 obj.hasOwnProperty('foo'); // ERROR! hasOwnProperty is not a function
415 hasOwn(obj, 'foo'); // true
416 hasOwn(obj, 'hasOwnProperty'); // true
417 hasOwn(obj, 'toString'); // false
424 Returns an array of all own enumerable properties found upon a given object.
425 It will use the native `Object.keys` if present.
427 PS: it won't return properties from the prototype.
429 See: [`forOwn()`](#forOwn), [`values()`](#values)
437 keys(obj); // ['foo', 'bar', 'lorem']
442 ## map(obj, callback, [thisObj]):Object
444 Returns a new object where the property values are the result of calling the
445 callback for each property in the original object, similar to Array/map.
447 The callback function receives the same arguments as in `forOwn()`.
449 See: [`forOwn()`](#forOwn)
452 var obj = { foo: 1, bar: 2 },
453 data = { foo: 0, bar: 1 };
455 map(obj, function(v) { return v + 1; }); // { foo: 2, bar: 3 }
456 map(obj, function(v, k) { return k; }); // { foo: "foo", bar: "bar" }
457 map(obj, function(v, k) { return this[k]; }, data); // { foo: 0, bar: 1 }
462 ## matches(obj, props):Boolean
464 Checks if object contains all properties/values pairs. Useful for validation
468 var john = {age:25, hair:'long', beard:true};
469 var mark = {age:27, hair:'short', beard:false};
470 var hippie = {hair:'long', beard:true};
471 matches(john, hippie); // true
472 matches(mark, hippie); // false
475 See [`deepMatches()`](#deepMatches)
479 ## merge(...objects):Object
481 Deep merges objects. Note that objects and properties will be cloned during the
482 process to avoid undesired side effects. It return a new object and won't
483 affect source objects.
486 var obj1 = {a: {b: 1, c: 1, d: {e: 1, f: 1}}};
487 var obj2 = {a: {b: 2, d : {f : 'yeah'} }};
489 merge(obj1, obj2); // {a: {b : 2, c : 1, d : {e : 1, f : 'yeah'}}}
492 See: [`deepMixIn()`](#deepMixIn), [`deepFillIn()`](#deepFillIn)
496 ## max(obj[, iterator]):*
498 Returns maximum value inside object or use a custom iterator to define how
499 items should be compared. Similar to [Array/max](array.html#max).
504 max({a: 100, b: 2, c: 1, d: 3, e: 200}); // 200
505 max({a: 'foo', b: 'lorem', c: 'amet'}, function(val){
512 ## min(obj[, iterator]):*
514 Returns minimum value inside object or use a custom iterator to define how
515 items should be compared. Similar to [Array/min](array.html#min).
520 min({a: 100, b: 2, c: 1, d: 3, e: 200}); // 1
521 min({a: 'foo', b: 'lorem', c: 'amet'}, function(val){
528 ## mixIn(target, ...objects):Object
530 Combine properties from all the objects into first one.
532 This method affects target object in place, if you want to create a new Object
533 pass an empty object as first parameter.
537 1. `target` (Object) : Target Object.
538 2. `...objects` (...Object) : Objects to be combined (0...n objects).
543 var a = {foo: "bar"};
544 var b = {lorem: 123};
546 mixIn({}, a, b); // {foo: "bar", lorem: 123}
547 console.log(a); // {foo: "bar"}
549 mixIn(a, b); // {foo: "bar", lorem: 123}
550 console.log(a); // {foo: "bar", lorem: 123}
553 See: [`fillIn()`](#fillIn), [`merge()`](#merge)
558 ## namespace(obj, propName):Object
560 Creates an empty object inside namespace if not existent. Will return created
561 object or existing object.
563 See: [`get()`](#get), [`set()`](#set)
567 namespace(obj, 'foo.bar'); // {}
568 console.log(obj); // {foo:{bar:{}}}
572 ## omit(obj, ...keys):Object
574 Return a copy of the object without the blacklisted keys.
576 See: [`filter()`](#filter)
586 // can pass an array of keys as second argument
587 var keys = ['firstName', 'dob']
588 omit(user, keys); // {lastName : 'Doe', gender : 'male'}
590 // or multiple arguments
591 omit(user, 'firstName', 'lastName'); // {dob : '1985/07/23', gender : 'male'}
596 ## pick(obj, ...keys):Object
598 Return a copy of the object that contains only the whitelisted keys.
600 See: [`filter()`](#filter)
610 // can pass an array of keys as second argument
611 var keys = ['firstName', 'dob']
612 pick(user, keys); // {firstName:"John", dob: "1985/07/23"}
614 // or multiple arguments
615 pick(user, 'firstName', 'lastName'); // {firstName:"John", lastName: "Doe"}
620 ## pluck(obj, propName):Object
622 Extract an object containing property values with keys as they appear in the
637 pluck(users, 'name'); // {first: 'John', second: 'Mary'} );
638 pluck(users, 'age'); // {first: 21, second: 25} );
643 ## reduce(obj, callback, initial, [thisObj]):*
645 Similar to [Array/reduce](array.html#reduce).
647 Apply a function against an accumulator and each property of the object (order
648 is undefined) as to reduce it to a single value.
651 var obj = {a: 1, b: 2, c: 3, d: 4};
653 function sum(prev, cur, key, list) {
658 reduce(obj, sum); // 10
663 ## reject(obj, callback, thisObj):Object
665 Returns a new object containing all properties where `callback` returns true,
666 similar to [Array/reject](array.html#reject). It does not use properties from
667 the object's prototype. Opposite of [`filter()`](#filter).
669 See [`filter()`](#filter)
674 var obj = {a: 1, b: 2, c: 3, d: 4, e: 5};
675 reject(obj, function(x) { return (x % 2) !== 0; }); // {b: 2, d: 4}
682 Returns an array of all own enumerable properties values found upon a given object.
684 PS: it won't return properties from the prototype.
686 See: [`forOwn()`](#forOwn), [`keys()`](#keys)
694 values(obj); // [1, 2, 3]
699 ## set(obj, propName, value)
701 Sets a nested property value.
703 See: [`get()`](#get), [`namespace()`](#namespace)
707 set(obj, 'foo.bar', 123);
708 console.log(obj.foo.bar); // 123
709 console.log(obj); // {foo:{bar:123}}
716 Returns the count of own enumerable properties found upon a given object.
718 PS: it won't return properties from the prototype.
720 See: [`forOwn()`](#forOwn), [`keys()`](#keys)
733 ## some(obj, callback, [thisObj]):Boolean
735 Similar to [Array/some](array.html#some). Tests whether any properties in the
736 object pass the test implemented by the provided callback.
746 some(obj, isNumber); // true
751 ## unset(obj, propName):Boolean
753 Delete object property if existent and returns a boolean indicating succes. It
754 will also return `true` if property doesn't exist.
756 Some properties can't be deleted, to understand why [check this
757 article](http://perfectionkills.com/understanding-delete/).
770 unset(lorem, 'ipsum.dolor.sit'); // true
771 console.log(lorem.ipsum.dolor); // {}
772 unset(lorem, 'foo.bar'); // true
777 ## result(object, property):Mixed
779 Evaluates an objects property and returns result.
786 // some dynamic calculated property.
791 var name = result(person, 'name'), // john
792 mood = result(person, 'mood'); // happy