分配值,而不是在javascript中引用 [英] Assign value not reference in javascript
本文介绍了分配值,而不是在javascript中引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在用javascript分配对象时遇到了一些问题.
I am having a little problem assigning objects in javascript.
看看这个示例代码,它再现了我的问题.
take a look at this sample code that reproduces my problem.
var fruit = {
name: "Apple"
};
var vegetable = fruit;
vegetable.name = "potatoe";
console.log(fruit);
它记录
Object {name: "potatoe"}
如何将值(不是对象的引用)分配给另一个对象?
How can I assign the value not the reference of an object to another object?
推荐答案
var fruit = {
name: "Apple"
};
var vegetable = Object.assign({}, fruit);
vegetable.name = "potatoe";
console.log(fruit);
这篇关于分配值,而不是在javascript中引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文