mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
enhance #extend to allow taking multiple source objects.
This is done in jquery, extjs, and others as well. added tests, no docs
This commit is contained in:
committed by
Jeremy Ashkenas
parent
8c6694c242
commit
4085b68be4
@@ -25,9 +25,14 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("objects: extend", function() {
|
test("objects: extend", function() {
|
||||||
var source = {name : 'moe'}, dest = {age : 50};
|
var result;
|
||||||
_.extend(dest, source);
|
equals(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another');
|
||||||
equals(dest.name, 'moe', 'can extend an object with the attributes of another');
|
equals(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination');
|
||||||
|
equals(_.extend({x:'x'}, {a:'b'}).x, 'x', 'properties not in source dont get overriden');
|
||||||
|
result = _.extend({x:'x'}, {a:'a'}, {b:'b'});
|
||||||
|
ok(_.isEqual(result, {x:'x', a:'a', b:'b'}), 'can extend from multiple source objects');
|
||||||
|
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
|
||||||
|
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("objects: clone", function() {
|
test("objects: clone", function() {
|
||||||
|
|||||||
@@ -432,10 +432,13 @@
|
|||||||
return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extend a given object with all of the properties in a source object.
|
// Extend a given object with all the properties in given object(s)
|
||||||
_.extend = function(destination, source) {
|
_.extend = function(obj) {
|
||||||
for (var property in source) destination[property] = source[property];
|
var prop;
|
||||||
return destination;
|
each(_.rest(arguments), function (source) {
|
||||||
|
for (prop in source) obj[prop] = source[prop];
|
||||||
|
});
|
||||||
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a (shallow-cloned) duplicate of an object.
|
// Create a (shallow-cloned) duplicate of an object.
|
||||||
|
|||||||
Reference in New Issue
Block a user