0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

added clone method to common, extend now has option to disable deep

This commit is contained in:
liabru 2014-03-10 19:56:17 +00:00
parent 5e65166faa
commit fc6ee29ed4

View file

@ -11,21 +11,34 @@ var Common = {};
/** /**
* Description * Description
* @method extend * @method extend
* @param {} obj, obj, obj... * @param {} obj
* @param {boolean} deep
* @return {} obj extended * @return {} obj extended
*/ */
Common.extend = function(obj) { Common.extend = function(obj, deep) {
var args = Array.prototype.slice.call(arguments, 1); var argsStart,
args,
deepClone;
if (typeof deep === 'boolean') {
argsStart = 2;
deepClone = deep;
} else {
argsStart = 1;
deepClone = true;
}
args = Array.prototype.slice.call(arguments, argsStart);
for (var i = 0; i < args.length; i++) { for (var i = 0; i < args.length; i++) {
var source = args[i]; var source = args[i];
if (source) { if (source) {
for (var prop in source) { for (var prop in source) {
if (source[prop].constructor === Object) { if (deepClone && source[prop].constructor === Object) {
if (!obj[prop] || obj[prop].constructor === Object) { if (!obj[prop] || obj[prop].constructor === Object) {
obj[prop] = obj[prop] || {}; obj[prop] = obj[prop] || {};
Common.extend(obj[prop], source[prop]); Common.extend(obj[prop], deepClone, source[prop]);
} else { } else {
obj[prop] = source[prop]; obj[prop] = source[prop];
} }
@ -39,6 +52,17 @@ var Common = {};
return obj; return obj;
}; };
/**
* Creates a new clone of the object, if deep is true references will also be cloned
* @method clone
* @param {} obj
* @param {bool} deep
* @return {} obj cloned
*/
Common.clone = function(obj, deep) {
return Common.extend({}, deep, obj);
};
/** /**
* Description * Description
* @method keys * @method keys