在类中没有Spread运算符的情况下合并对象属性

时间:2018-05-18 06:29:52

标签: javascript performance ecmascript-6

我试图在没有Prototype patternes6 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); 

0 个答案:

没有答案