Created
January 2, 2012 18:03
-
-
Save kwhinnery/1551558 to your computer and use it in GitHub Desktop.
Mixin implementation for Titanium 1.8 - supports shallow or deep copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mixin = require('mixin'); | |
var o1 = { | |
foo:{ | |
something:'osauhduisdfb', | |
bar:'baz' | |
} | |
}; | |
var o2 = { | |
foo:{ | |
bar:'hello' | |
} | |
}; | |
var o3 = { | |
hey:'dude' | |
}; | |
mixin(o1,o2,o3); | |
alert(JSON.stringify(o1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Mixin properties of n objects into the given object - pass in as many objects to mix in as you like. | |
* Can perform a shallow or deep copy (deep is default). | |
* | |
* Usage: | |
* mixin([Boolean deepCopy,] objectToExtend, objectToMixIn [, as many other objects as needed]) | |
* | |
* Examples: | |
* | |
* var o1 = { | |
* foo:'bar', | |
* anObject: { | |
* some:'value' | |
* clobber:false | |
* } | |
* }; | |
* | |
* var o2 = { | |
* foo:'bar', | |
* aNewProperty:'something', | |
* anObject: { | |
* some:'other value' | |
* } | |
* }; | |
* | |
* //merge properties of o2 into o1 | |
* mixin(o1,o2) | |
* | |
* //clone o1: | |
* var clone = mixin({},o1); | |
* alert(clone.foo); //gives 'bar' | |
* | |
*/ | |
//helper function for mixin - adapted from jQuery core | |
function _isPlainObject(obj) { | |
if(!obj || typeof obj !== 'object' || obj.nodeType) { | |
return false; | |
} | |
try { | |
// Not own constructor property must be Object | |
if(obj.constructor && !Object.prototype.hasOwnProperty.call(obj, "constructor") && !Object.prototype.hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) { | |
return false; | |
} | |
} catch ( e ) { | |
return false; | |
} | |
// Own properties are enumerated firstly, so to speed up, | |
// if last one is own, then all properties are own. | |
var key; | |
for(key in obj ) {} | |
return key === undefined || Object.prototype.hasOwnProperty.call(obj, key); | |
} | |
//helper for main mixin function interface, defined below this function as "mixin" | |
function _merge(obj1, obj2, recursive) { | |
for(var p in obj2) { | |
if(obj2.hasOwnProperty(p)) { | |
try { | |
if(recursive && _isPlainObject(obj2[p])) { | |
obj1[p] = _merge(obj1[p], obj2[p]); | |
} | |
else { | |
obj1[p] = obj2[p]; | |
} | |
} | |
catch(e) { | |
obj1[p] = obj2[p]; | |
} | |
} | |
} | |
return obj1; | |
} | |
//main interface, exposed in module | |
function mixin() { | |
var obj, mixins, deep = true; | |
if (typeof arguments[0] === 'boolean') { | |
deep = arguments[0]; | |
obj = arguments[1]; | |
mixins = Array.prototype.slice.call(arguments,2); | |
} | |
else { | |
obj = arguments[0]; | |
mixins = Array.prototype.slice.call(arguments,1); | |
} | |
//mix in properties of given objects | |
for (var i = 0, l = mixins.length; i<l; i++) { | |
_merge(obj,mixins[i],deep); | |
} | |
return obj; | |
} | |
module.exports = mixin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment