【发布时间】:2019-03-14 13:33:30
【问题描述】:
我有一个“房子”类,比如:
class House{
constructor(params){
this.clear();
// this = {...params} // I know that don't work !!!
//--
// if(params.address !== undefined) this.address = {...params.address}
//...
}
clear(){
this.address = {
number: null,
street: null,
zipcode: null,
ton: null,
}
this.access = {
doorcode: null,
stair: null,
}
}
}
我想创建一个新的 House 实例并在构造函数中注入多个 json,例如:
const h = new House({address: { /* json */ }, access: { /* json */});
或者只有一个:
const h = new House({access: { /* json */});
在构造函数中,我是否有义务检查“参数”中的所有值以插入良好的属性(嵌套对象)
我想避免创建其他类,如地址和访问,并在房屋构造函数中创建每个类的新实例。 最佳做法是什么?
问候
【问题讨论】:
-
Object.assign(this,params)工作吗?
标签: javascript json class object