Javascript 使用方法从类变量更新对象

Javascript 使用方法从类变量更新对象,javascript,typescript,object,Javascript,Typescript,Object,我的目标是创建设置了默认属性的对象数组,但在某些情况下,我需要更新一个或多个属性。 下面是我的示例类,其中包含变量-对象 export class Components{ public static component_1 = { width: 1, height: 2, x: 3, y:4 } public static component_2 = { width: 10,

我的目标是创建设置了默认属性的对象数组,但在某些情况下,我需要更新一个或多个属性。 下面是我的示例类,其中包含变量-对象

export class Components{
    public static component_1 = {
        width: 1,
        height: 2,
        x: 3,
        y:4
    }
    
    public static component_2 = {
        width: 10,
        height: 20,
        x: 30,
        y:40
    }

    ...
}
我想以以下方式创建我的对象数组:

让myArrOfComponents=[Components.component_1,Components.component_2,…]

问题是,在某些情况下,我必须更新object中的一个或多个属性,例如width和x。例如,我想这样做

let myArrOfComponents = [Components.component_1.configure(width = 99, x=88)]

你能帮我怎么做吗?我不知道如何实现“.configure()”我正试图解决这个问题,但不知道如何实现它或类似的行为

最简单的选择是使它们成为方法(所谓的“工厂方法”)


现在,
Components.component_1()
将返回一个默认对象,例如
Components.component_1({x:25,width:100})
a“配置”对象。

{…Components.component_1,width:99,x:88}
应该可以。如果这些都是实例,你可以给它们一个克隆+更新的方法,包括做更高级的事情。传递一个对象作为参数,JS没有命名参数。它看起来很棒!我会试试的!:)
class Components {
    static component_1 = (options = null) => ({
        width: 1,
        height: 2,
        x: 3,
        y: 4,
        ...options
    })
}