简体   繁体   English

Javascript 构造函数创建与先前创建的具有相同值的新对象 object

[英]Javascript constructor creating new objects with same values as previously created object

Here is my (simplified) constructor:这是我的(简化的)构造函数:

class User {
    constructor() {
        Object.assign(this, setup); 
    }
}

const setup = {
    name: ''
}

export { User }

When I create a new object当我创建一个新的 object

import {
    User
} from './User.js';

let user1 = new User();

user1 is what I expect. user1是我所期望的。

If I declare user1.name = 'John' and then create another user let user2 = new User() then user2.name will be John, not an empty string.如果我声明user1.name = 'John'然后创建另一个用户let user2 = new User()那么user2.name将是 John,而不是空字符串。

Can someone explain why?有人可以解释为什么吗?

It appears setup is being changed and referenced instead of the new user object that was created.似乎正在更改和引用setup ,而不是创建的新用户 object。

It appears setup is being changed and referenced instead of the new user object that was created.似乎正在更改和引用setup ,而不是创建的新用户 object。

Yes.是的。 You need to make a copy of object before assigning it to instance of class在将其分配给 class 实例之前,您需要复制 object

class User {
    constructor() {
        Object.assign(this, {...setup}); 
    }
}

For anyone else running into similar issues, I finally figured it out.对于遇到类似问题的其他人,我终于弄清楚了。 It boils down to shallow copying versus deep copying in Javascript.它归结为 Javascript 中的浅拷贝与深拷贝。 Both spread (...) and Object.assign() perform a shallow copy while JSON methods perform a deep copy. spread (...)Object.assign()都执行浅拷贝,而 JSON 方法执行深拷贝。

If your setup is a nested object such as如果您的设置是嵌套的 object,例如

const setup = {
    name: '',
    address: {
        city: ''
    }
}

the address object inside of setup will remain a referenced value, not a primitive value, when {...} or Object.assign() is used.当使用{...}Object.assign()时, setup内部的address object 将保持引用值,而不是原始值。

If you want to copy the whole object, you need to copy it as JSON.parse(JSON.stringify(setup)) so my constructor should be如果要复制整个 object,则需要将其复制为JSON.parse(JSON.stringify(setup))所以我的构造函数应该是

class User {
    constructor() {
        Object.assign(this, JSON.parse(JSON.stringify(setup))); 
    }
}

since this performs a deep (nested) copy.因为这会执行深层(嵌套)复制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么对象构造函数中的函数会更改该构造函数先前创建的所有对象? - Why is a function within an object constructor altering all objects previously created by that constructor? 什么时候在JavaScript的构造函数和类中创建新对象? - When are new objects created in JavaScript's constructor function vs in class? 我可以在由javascript中相同构造函数创建的其他对象上调用方法吗? - Can I call a method on other objects created by the same constructor in javascript? 使用Javascript中使用相同函数构造函数创建的所有对象实例的数组 - Array with all Instances of Objects created with the same function constructor in Javascript 由new构造函数创建的函数对象是否在javascript中被视为可变对象? - Is function object created by `new` constructor treated as mutable object in javascript? 用javascript中另一个对象的值创建新对象 - creating new object with values of another object in javascript JavaScript:基于原型创建对象而不使用new + Constructor - JavaScript: Creating objects based on a prototype without using new + Constructor 带有对象的javascript对象构造函数 - javascript object constructor with objects 为什么用构造函数创建对象会在javascript中执行对象的方法? - Why does creating objects with constructor execute object's method in javascript? 如何将新字段添加到之前创建的 TypeScript\JavaScript object 中? - How can I add a new field into a TypeScript\JavaScript object previously created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM