We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关键词:结构复制对象
浅拷贝
举例:
const obj = { prop1: "value1", prop2: { nestedProp: "nestedValue", }, }; // 使用扩展运算符进行复制 const obj2 = { ...obj }; console.log("原始对象 obj:", obj); console.log("复制后的对象 obj2:", obj2); // 修改基本类型属性 obj2.prop1 = "newValue1"; console.log("修改基本类型属性后:"); console.log("原始对象 obj:", obj); console.log("复制后的对象 obj2:", obj2); // 修改嵌套对象的属性 obj2.prop2.nestedProp = "newNestedValue"; console.log("修改嵌套对象属性后:"); console.log("原始对象 obj:", obj); console.log("复制后的对象 obj2:", obj2);
解释如下:
obj
prop1
prop2
{...obj}
obj2
prop2.nestedProp
The text was updated successfully, but these errors were encountered:
No branches or pull requests
关键词:结构复制对象
浅拷贝
举例:
解释如下:
obj
,它包含一个基本类型属性prop1
和一个嵌套对象属性prop2
。{...obj}
创建了一个新的对象obj2
,这看起来像是对obj
进行了复制。obj2
的基本类型属性prop1
时,原始对象obj
的prop1
不受影响。这是因为基本类型的值在复制时是按值复制的。obj2
的嵌套对象属性prop2.nestedProp
时,原始对象obj
的prop2.nestedProp
也被修改了。这是因为扩展运算符对于嵌套对象只是复制了引用,而不是创建一个全新的嵌套对象副本,所以这是浅拷贝的行为。The text was updated successfully, but these errors were encountered: