对象拷贝

对象拷贝

JavaScript对象浅拷贝和深拷贝

javascript/jQueryboloog 发表了文章 • 1 个评论 • 996 次浏览 • 2017-04-25 19:01 • 来自相关话题

创建一个对象
var obj = {
name:'柏龙',
age: 30,
sex: '男',
hobby: ['writeCode','playGame','readBook'],
friend: {
name: 'luobo',
age: 20
}
}
?浅拷贝实例代码
?
function shallowCopy(oldObj) {
var newObj = {};
for(var i in oldObj) {
if(oldObj.hasOwnProperty(i)) {
newObj[i] = oldObj[i];
}
}
return newObj;
}

var copy1 = shallowCopy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy1);
深拷贝实例代码1
?
function copy(oldObj){
var newObj = {};
for(var key in oldObj){
if(typeof oldObj[key] === 'string' || typeof oldObj[key] === 'number' || typeof oldObj[key] === 'boolean' || typeof oldObj[key] === 'undefined' || oldObj[key] === null ){
newObj[key] = oldObj[key];
}else{
newObj[key] = copy(oldObj[key]);
}
}
return newObj;
}
var copy2 = copy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy2);
?
深拷贝实例代码2
?
function setCopy(oldObj){
var newObj = JSON.parse(JSON.stringify(oldObj));
return newObj;
}
var copy3 = setCopy(obj);
copy3.name = 'xiaoqiao';
copy3.age = 22;
copy3.friend.name = '帮主';
obj.name = 'bsfans';
console.log(obj);
console.log(copy3);
?
?
?
?
?
?
?
?
? 查看全部
创建一个对象
var obj = {
name:'柏龙',
age: 30,
sex: '男',
hobby: ['writeCode','playGame','readBook'],
friend: {
name: 'luobo',
age: 20
}
}

?浅拷贝实例代码
?
function shallowCopy(oldObj) {
var newObj = {};
for(var i in oldObj) {
if(oldObj.hasOwnProperty(i)) {
newObj[i] = oldObj[i];
}
}
return newObj;
}

var copy1 = shallowCopy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy1);

深拷贝实例代码1
?
function copy(oldObj){
var newObj = {};
for(var key in oldObj){
if(typeof oldObj[key] === 'string' || typeof oldObj[key] === 'number' || typeof oldObj[key] === 'boolean' || typeof oldObj[key] === 'undefined' || oldObj[key] === null ){
newObj[key] = oldObj[key];
}else{
newObj[key] = copy(oldObj[key]);
}
}
return newObj;
}
var copy2 = copy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy2);

?
深拷贝实例代码2
?
function setCopy(oldObj){
var newObj = JSON.parse(JSON.stringify(oldObj));
return newObj;
}
var copy3 = setCopy(obj);
copy3.name = 'xiaoqiao';
copy3.age = 22;
copy3.friend.name = '帮主';
obj.name = 'bsfans';
console.log(obj);
console.log(copy3);

?
?
?
?
?
?
?
?
?

JavaScript对象浅拷贝和深拷贝

javascript/jQueryboloog 发表了文章 • 1 个评论 • 996 次浏览 • 2017-04-25 19:01 • 来自相关话题

创建一个对象
var obj = {
name:'柏龙',
age: 30,
sex: '男',
hobby: ['writeCode','playGame','readBook'],
friend: {
name: 'luobo',
age: 20
}
}
?浅拷贝实例代码
?
function shallowCopy(oldObj) {
var newObj = {};
for(var i in oldObj) {
if(oldObj.hasOwnProperty(i)) {
newObj[i] = oldObj[i];
}
}
return newObj;
}

var copy1 = shallowCopy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy1);
深拷贝实例代码1
?
function copy(oldObj){
var newObj = {};
for(var key in oldObj){
if(typeof oldObj[key] === 'string' || typeof oldObj[key] === 'number' || typeof oldObj[key] === 'boolean' || typeof oldObj[key] === 'undefined' || oldObj[key] === null ){
newObj[key] = oldObj[key];
}else{
newObj[key] = copy(oldObj[key]);
}
}
return newObj;
}
var copy2 = copy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy2);
?
深拷贝实例代码2
?
function setCopy(oldObj){
var newObj = JSON.parse(JSON.stringify(oldObj));
return newObj;
}
var copy3 = setCopy(obj);
copy3.name = 'xiaoqiao';
copy3.age = 22;
copy3.friend.name = '帮主';
obj.name = 'bsfans';
console.log(obj);
console.log(copy3);
?
?
?
?
?
?
?
?
? 查看全部
创建一个对象
var obj = {
name:'柏龙',
age: 30,
sex: '男',
hobby: ['writeCode','playGame','readBook'],
friend: {
name: 'luobo',
age: 20
}
}

?浅拷贝实例代码
?
function shallowCopy(oldObj) {
var newObj = {};
for(var i in oldObj) {
if(oldObj.hasOwnProperty(i)) {
newObj[i] = oldObj[i];
}
}
return newObj;
}

var copy1 = shallowCopy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy1);

深拷贝实例代码1
?
function copy(oldObj){
var newObj = {};
for(var key in oldObj){
if(typeof oldObj[key] === 'string' || typeof oldObj[key] === 'number' || typeof oldObj[key] === 'boolean' || typeof oldObj[key] === 'undefined' || oldObj[key] === null ){
newObj[key] = oldObj[key];
}else{
newObj[key] = copy(oldObj[key]);
}
}
return newObj;
}
var copy2 = copy(obj);
obj.friend.name = '萝卜';
console.log(obj);
console.log(copy2);

?
深拷贝实例代码2
?
function setCopy(oldObj){
var newObj = JSON.parse(JSON.stringify(oldObj));
return newObj;
}
var copy3 = setCopy(obj);
copy3.name = 'xiaoqiao';
copy3.age = 22;
copy3.friend.name = '帮主';
obj.name = 'bsfans';
console.log(obj);
console.log(copy3);

?
?
?
?
?
?
?
?
?