nlivaic
10/31/2018 - 9:25 PM

.bind()

.bind()

let o = {
  carId: 123,
  getId: function() {
    return this.carId;             // Note 'this' keyword.
  }
};

console.log(o.getId());            // 123

let newCar = { carId: 456 };
let newFn = o.getId.bind(newCar);  // .bind() creates a copy of getId()
console.log(newFn());              // which then gets called with a new context.
                                   // 456