XAMPPRocky
XAMPPRocky

Reputation: 3599

ES6/Babel version of binding to function of an object

How would I implement the same behaviour as below in es6

var grand_parent = {
  parent: {
     child: function () {
       // logic
     }.bind(this)
  }
}

I tired the following, but I got a syntax error.

var grand_parent = {
   parent: {
    child() {
      // logic
    }.bind(this)
  }

Upvotes: 0

Views: 339

Answers (1)

Bergi
Bergi

Reputation: 664385

You would most likely use an arrow function:

var grand_parent = {
  parent: {
    child: () => {
      // logic
    }
  }
};

where this is lexically bound and works just as in your .bind(this) scenario.
You cannot call .bind() using the method syntax.

Upvotes: 2

Related Questions