JavaScript对象拷贝的方法
一.json方法
1.适合情况:
json对象的深度克隆。方法是JSON.stringify()转为json字符串,再JSON.paarse()转为json数组
2.缺点:
1,如果你的对象里有函数,函数无法被拷贝下来
2,无法拷贝copyObj对象原型链上的属性和方法
var obj ={
x:1,
y:{
a:1,
b:0,
c:[1,2,3]
}
};
// 相同的引用
var obj2=obj;
console.log(obj2==obj); //true直接复制只是复制对象的指针,还指向同一个对象
// 不同的引用
var obj3 = JSON.parse(JSON.stringify(obj));
二.jQuery.extend方法
jQuery.extend(object)
概述:
扩展jQuery对象本身,Yoon过来在jQuery命名空间上增加新函数
var obj = {
x: 1,
y: {
a: 1,
b: 0,
c: [1, 2, 3]
}
};
var obj2 = $.extend({}, obj);
console.log(obj2 == obj) //false 复制后的地址不一样
console.log(obj2);
三.object.create()方法
复制对象存在于object原型prototype中
var obj = {
x: 1,
y: {
a: 1,
b: 0,
c: [1, 2, 3]
}
};
var obj2 = Object.create(obj);
console.log(obj2 == obj); //false
console.log(obj2);
四.for循环遍历方法
1.浅拷贝:
只是拷贝了基本类型的数据;然后引用类型数据,只是复制了指针,复制后也是会发生引用。
var obj={
x:1,
y:{
a:1,
b:0,
c:[1,2,3]
}
}
var obj2 ={};
// for in 会遍历对象的属性,包括实例中和原型中的属性。(需要可访问,可枚举属性)
for(var i in obj){
obj2[i]= obj[i];
}
console.log(obj2);
// 给新数组添加一个元素4,会同步反映在新旧数组中
obj2.y.c.push(4)
console.log(obj2.y.c)
console.log(obj.y.c) // 浅拷贝只是复制了地址,修改是内存中的数据
2.深拷贝
深拷贝。就是遍历那个被拷贝的对象,判断对象里每一项的数据类型。如果不是对象类型,就直接复制,如果是对象类型,就在此调用递归的方法去赋值
var obj={
x:1,
y:{
a:1,
b:0,
c:[1,2,3]
}
}
function getClass(o){ // 判断数据类型
return Object.prototype.toString.call(o).slice(8,-1);
}
function deepCopy(obj){
var result,oClass = getClass(obj);
if(oClass=="object") result ={}; //判断传入的如果是对象,继续遍历
else if(oClass =="object") result={}; // 判断传入的是数组,继续遍历
else return obj; //如果是基本类型就直接返回
for (var i in obj){
var copy = obj[i];
if(getClass(copy)=="object") result[i] = deepCopy(copy); // 递归方法,如果对象继续变量obj[i],下一级还是对象,就obj[i][i]
else if(getClass(copy)=="Array") result[i] =deepCopy(copy); //递归方法,如果对象继续数组obj[i],下一级还是数组,就[obj][i][i]
else result[i] = copy; // 基本数据类型则赋值给属性
}
return result
}
var obj2 = deepCopy(obj);
console.log(obj2)
五.原型链继承方法
function Father(){
this.say = "hi";
this.fn = function(){
return this.say;
}
}
Father.prototype.eat = function(){
console.log('吃');
}
function Son(){
this.play = function(){
console.log('play game');
}
}
// 通过原型来继承父类的公共属性
Son.prototype = new Father();
var s = new Son;
console.log(s);
console.log(s.say);
console.log(s.fn());