简体   繁体   English

如何在 JavaScript 中将对象传播到类属性中

[英]How to spread an object into a classes properties in JavaScript

Basically here's what I'm trying to accomplish.基本上这就是我想要完成的。

 class Person { constructor (obj) { this.first = '' this.last = '' this.age = '' if (obj) { Object.assign(this, ...obj) } } } const a = new Person() console.log('Not spreading: ', a) const b = new Person({ first: 'Alex', last: 'Cory', age: 27 }) console.log('Spreading: ', b)

Is there a way to spread an object like this to populate a class?有没有办法传播这样的对象来填充一个类?

If you're using Object.assign , you don't use spread notation; 如果您使用的是Object.assign ,则不使用扩展表示法; just remove the ... : 只需删除...

 class Person { constructor (obj) { this.first = '' this.last = '' this.age = '' if (obj) { Object.assign(this, obj) // <============ No ... } } } const a = new Person() console.log('Not spreading: ', a) const b = new Person({ first: 'Alex', last: 'Cory', age: 27 }) console.log('Spreading: ', b) 

There is a proposal (currently at Stage 3, so likely to be in ES2018, and widely supported by transpilers) that does object property spread in object initializers, but that wouldn't apply to your situation where the object already exists. 有一个提议 (目前处于第3阶段,很可能在ES2018中,并得到转发器的广泛支持),它在对象初始化器中对象属性传播,但这不适用于对象已存在的情况。

You could use deconstruction and take only the properties, you need. 您可以使用解构并仅使用您需要的属性。

 class Person { constructor ({ first = '', last = '', age = '' } = {}) { Object.assign(this, { first, last, age }); } } const a = new Person() console.log('Not spreading: ', a) const b = new Person({ first: 'Alex', last: 'Cory', age: 27, foo: 42 }) console.log('Spreading: ', b) 

Is this what you're looking for? 这是你在找什么?

 class Person { constructor (obj) { this.firstName = '' this.lastName = '' this.age = '' if (obj) { Object.assign(this, obj) } } } const a = new Person() console.log('Not spreading: ', a) const b = new Person({ firstName: 'Alex', lastName: 'Cory', age: 27 }) console.log('Spreading: ', b) 

I would personally prefer to use a seperate method as it is not possible to have multiple constructors in JS.我个人更喜欢使用单独的方法,因为在 JS 中不可能有多个构造函数。 In the following example I create new objects using a static fromObject() method which returns a new object.在以下示例中,我使用返回新对象的static fromObject()方法创建新对象。 Therefore you can keep your normal constructor and create new objects using spread syntax as well.因此,您可以保留普通构造函数并使用扩展语法创建新对象。

Note: I am using typescript here.注意:我在这里使用打字稿。

export class Point {
    readonly x: number
    readonly y: number

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    static fromObject({x, y}: {x: number, y: number}) {
        return new Point(x, y)
    }
}

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

相关问题 如何在 Object 中传播嵌套属性? - How to spread nested properties in Object? 如何在 Javascript 中传播嵌套的 object? - How to spread a nested object in Javascript? 如何使用带有嵌套属性的 object 传播? - How to use object spread with nested properties? Javascript:将 object 的数组散布在 object 中,仅更改其中的属性 - Javascript: Spread array of an object in an object and change properties inside it only 如何使用扩展语法从 JavaScript 中的多个未知对象复制 object 属性? - How can I use spread syntax to copy object properties from multiple unknown objects in JavaScript? 传播具有动态属性的嵌套javascript对象的语法以进行分组 - Spread syntax for nested javascript object with dynamic properties for grouping JavaScript - 在三元语句中使用扩展语法向 object 添加属性 - JavaScript - adding properties to an object using the spread syntax within a ternary statement 如何使用对象解构扩展运算符来删除多个对象属性 - How to use object destructuring spread operator to delete multiple object properties Vue将对象传播为计算属性 - Vue spread an object as computed properties 如何在javascript中更改对象属性之一传播到另一个对象的数组 - How to change array that one of the objects properties spread in to another in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM