/**
* 继承方法1 浅拷贝
* @param {[type]} obj [父对象]
* @return {[type]} [返回新对象]
*/
function cloneObj(obj){
var F = function() {};
F.prototype = obj;
return new F();
}
/**
* 继承方法2 浅拷贝
* @param {[type]} obj [父对象]
* @return {[type]} [返回新对象]
*/
function extend(obj) {
var newObj = {};
for(attr in obj){
newObj[attr] = obj[attr];
}
return newObj;
}
/**
* 继承方法3 浅拷贝
* @param {[type]} superObj [父对象]
* @param {[type]} subObj [子对象]
* @return {[type]} [返回新对象]
*/
function extend(superObj, subObj) {
var subObj = subObj || {};
for(prop in superObj){
subObj[prop] = superObj[prop];
}
return subObj;
}
/**
* 继承方法3 深拷贝 递归
* @param {[type]} superObj [父对象]
* @param {[type]} subObj [子对象]
* @return {[type]} [返回新对象]
*/
function extendDeeply(superObj, subObj) {
var subObj = subObj || {};
for(prop in superObj){
if(typeof superObj[prop] == "object") {
superObj[prop] = (superObj[prop].construtor === Array) ? [] : {};
extendDeeply(superObj[prop], superObj[prop]);
}else{
subObj[prop] = superObj[prop];
}
}
return subObj;
}
/**
* 继承方法4 深拷贝 递归方式2
* @param {[type]} obj [拷贝对象]
* @return {[type]} [返回新对象]
*/
function deepCopy(obj) {
if( typeof obj != 'object' ) {
return obj;
}
var newObj = {};
for(var attr in obj) {
newObj[attr] = deepCopy(obj[attr]);
}
return newObj;
}
网友评论