Skip to content

Instantly share code, notes, and snippets.

@131
Last active October 11, 2016 22:53
Show Gist options
  • Save 131/4c782158e61921323a9445fbe13563d4 to your computer and use it in GitHub Desktop.
Save 131/4c782158e61921323a9445fbe13563d4 to your computer and use it in GitHub Desktop.
Static context in ES6 classes

Long story short, in javascript "this" refer to the thing-on-the-left-side-of-the-dot. period.

So, when writing ES6 classes & statics methods, use "this" to refer to the static classe.

class User {

  static find(criteria) {

      //nothing special
    var users_ids = sql.search(criteria).select('user_id');
      //still good here


    users_ids.map( id => return this.instanciate(id) ); //wait what ?
  }

  static instanciate(id){
    return new this(); //     this     is     javascript
  }
}


var joes = User.find("user_name LIKE 'joe%'");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment