Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Created June 3, 2014 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangjunjun/18e5a62577ba634f6bc5 to your computer and use it in GitHub Desktop.
Save yangjunjun/18e5a62577ba634f6bc5 to your computer and use it in GitHub Desktop.
extend 扩展
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