1 varOP =Object.prototype,2Ostring =op.tostring,3Hasown =Op.hasownproperty;4 5 functionIsfunction (IT) {6 returnOstring.call (IT) = = = ' [Object Function] ';7 };8 9 functionIsArray (IT) {Ten returnOstring.call (IT) = = = ' [Object Array] '; One }; A functionHasprop (obj, prop) { - returnHasown.call (obj, prop); - }; the functionEachprop (obj, func) { - varprop; - for(Propinchobj) { - if(Hasprop (obj, prop)) { + if(func (Obj[prop], prop)) { - Break; + } A } at } - }; - /** - * Simple function to mix in properties from source into target, - * But only if Target does not already has a property of the the same name. - * in * @param {target} destination object - * Source object @param {source} to * @param {force} enforces overriding properties of the target object + * @param {deepstringmixin} is a deep copy recursive operation - * the * @returns {target} * */ $ functionmixin (target, source, Force, deepstringmixin) {Panax Notoginseng if(source) { -Eachprop (Source,function(value, prop) { the if(Force | |!)Hasprop (target, prop)) { + if(Deepstringmixin &&typeofValue = = = ' object ' && value && A!isarray (value) &&!isfunction (value) && the! (ValueinstanceofRegExp)) { + - if(!Target[prop]) { $Target[prop] = {}; $ } - mixin (Target[prop], value, Force, deepstringmixin); -}Else { theTarget[prop] =value; - }Wuyi } the }); - } Wu returnTarget; - }; About $ - Call: - varobj = { -Name: ' Tom ', AAge:19, + children:{ theName: ' Jack ', -Age:30 $ } the }; the varObj2 = Mixin ({},obj,false,true); theObj.children.name= ' Marry '; theConsole.log (OBJ2);
JS Object Deep Dive copy (keyed out from Requirejs)