Created
June 3, 2014 15:13
-
-
Save yangjunjun/18e5a62577ba634f6bc5 to your computer and use it in GitHub Desktop.
extend 扩展
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
function extend(a, b) { | |
var b = b || {}; | |
for (var prop in b) { | |
if (Object.prototype.toString.call(b[prop]) === '[object Array]') { | |
a[prop] = []; | |
extend(a[prop], b[prop]); | |
} | |
else if(Object.prototype.toString.call(b[prop]) === '[object Object]') { | |
a[prop] = {}; | |
extend(a[prop], b[prop]); | |
} | |
else{ | |
a[prop] = b[prop]; | |
} | |
} | |
return a; | |
} | |
// | |
// test | |
// | |
var a = {}; | |
var b = { | |
index : 1, | |
color: ['red', 'yellow', 'pink'], | |
person:{ | |
name:'Bob', | |
age:'26' | |
} | |
} | |
console.log(a); | |
extend(a, b); | |
console.log(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment