javascript - es6 class pass this to a static class functions -


i have class

import { objectutilities } '~/utils/';  class object{     constructor(object) {          object.assign(this, { ...object });      }      utils = objectutilities; } 

and class statis method (class contains many static methods)

class objectutilities {    static getkey(object){     return object.key;   } } 

and want know if possible share "this" object class static method "getkey(object)"

want as:

let x = new object(object); x.utils.getkey(this); 

(objectutilities many static funcs dont want each of them)

thanks helping me out...

you can add constructor objectutilities class bind given context getkey function:

class objectutilities {    constructor(_this) {      this.getkey = this.getkey.bind(_this);    }       getkey() {      return this.key;    }  }    class myobject {      constructor(object) {          object.assign(this, { ...object });          this.utils = new objectutilities(this);      }  }    const objectfoo = { key: 'foo' };  const objectbar = { key: 'bar' };    let x = new myobject(objectfoo);  let y = new myobject(objectbar);    console.log(x.utils.getkey(), y.utils.getkey());


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -