javascript - How to assign cloned functions to new this instance -


i have following code, written in es6 plus stage 3 proposals:

class parent {   constructor(x){     this.x = x;     this.otherproperty = "preserve across copy";   }    printx=()=>{     console.log(this.x);   }    squarex=()=>{     this.x = this.x *this.x;    } }  class child extends parent {   constructor(x){     super(x);   }   }  const c = new child(20); const copy = {...c, x: 10};  console.log(json.stringify(c)); c.squarex(); console.log(json.stringify(c));  console.log(json.stringify(copy)); copy.squarex(); console.log(json.stringify(copy)); 

live demo: https://jsbin.com/wuqenabaya/1/edit?js,console

the idea create copy of instance c while updating of it's properties. output of code is:

{x:  20, otherproperty: "preserve across copy"} {x: 400, otherproperty: "preserve across copy"} {x:  10, otherproperty: "preserve across copy"} {x:  10, otherproperty: "preserve across copy"} 

so can see copy.squarex() not update instance copy. issue function squarex() still bound old instance c.

what want have last call squarex() update instance copy. how can achieved?

edit: i'm using babel following plugins, allow use of new js features (spread, function props).

{   "presets": [     ["es2015", { "modules": false }],     "stage-0",     "react"   ],   "plugins": [     "react-hot-loader/babel",     "transform-object-rest-spread",     "transform-class-properties",     "transform-flow-strip-types"   ] } 

there few problems trying use spread properties this, not least you'll end object prototype object.prototype, not child.prototype.

you're using field syntax methods, makes fields own properties of object, not prototype properties, there doesn't seem reason in case; use method syntax.

to copy, give "copy constructor" branch in constructor:

constructor(x, ...more){   if (x instanceof child) {     // copy constructor branch     super(x.x);     object.assign(this, x, ...more);   } else {     super(x);   } }   

or if prefer, create copy:

const copy = object.assign(new child(c.x), c, {x: 10}); 

in either case, if choose keep using fields rather methods, you'll have adjust things since otherwise you'll copy squarex , printx well.

live example using methods , copy constructor:

class parent {    constructor(x){      this.x = x;      this.otherproperty = "preserve across copy";    }      printx() {      console.log(this.x);    }      squarex() {      this.x = this.x *this.x;     }  }    class child extends parent {    constructor(x, ...more){      if (x instanceof child) {        super(x.x);        object.assign(this, x, ...more);      } else {        super(x);      }    }    }    const c = new child(20);  const copy = new child(c, {x: 10});    console.log(json.stringify(c));  c.squarex();  console.log(json.stringify(c));    console.log(json.stringify(copy));  copy.squarex();  console.log(json.stringify(copy));


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -