我试图在没有Prototype pattern的es6 class Spread Operator上实施Object.assign但没有成功。
我想在画布上创建10000000个星星,所以我认为这个模式将是完美的,因为按照定义
原型模式是软件中的创新设计模式 发展。在要创建的对象类型时使用它 由一个原型实例确定,该实例被克隆以产生新的 对象。
到目前为止,这是我使用Spread Operator的工作代码:
class Prototype {// Class
constructor(options) {// constructor
this.options = options;//properties
this.context = undefined;
}
//Methods
clone (features){
let clone = new Prototype(this.options);
clone.options = {...clone.options, ...features};
//Object.assign(clone.options, features);
return clone;
}
}
//object literal
var options ={
width : 200,
height: 100
}
let prototype = new Prototype(options);
let clone = prototype.clone({
height: 800,
color: "red"
} );
console.log("prototype:::",prototype,"clone:::",clone);
但是使用{{3}},我得到的这个根本不正确
class Prototype {// Class
constructor(options) {// constructor
this.options = options;//properties
this.context = undefined;
}
//Methods
clone (features){
let clone = new Prototype(this.options);
//clone.options = {...clone.options, ...features};
Object.assign(clone.options, features);
return clone;
}
}
//object literal
var options ={
width : 200,
height: 100
}
let prototype = new Prototype(options);
let clone = prototype.clone({
height: 800,
color: "red"
} );
console.log("prototype:::",prototype,"clone:::",clone);