Notes

this & bind

Edit on GitHub


ReactJS

this, .bind() Binding

  • Implicit Binding
  • Explicit Binding (call, apply, bind)
  • new Binding
  • window Binding

Explicit Binding

.apply() is the same as .call(), the only difference being you can pass an array of args instead of passing args one by one.

.bind() is like .call(), but it let’s you create a new function and invoke it later. .call() and .apply() are called/invoked immediately.

new Binding

What’s used in constructor functions..

1let Animal() {
2    // this = {}
3    this.color = color;
4    this.name = name;
5    this.type = type
6}
7
8let zebra = new Animal('black and white', 'Zorro, 'Zebra');

window Binding

1var sayAge() {
2    console.log(this.age);
3}
4
5var me = {
6    age: 25
7};
8
9sayAge();

If there is no binding for this, it binds it to the window object. 'use strict'; is usually good when you want this to error out instead of defaulting to window object