JS basis -this

this

this point, which has several situations?

this represents the function call associated with the object, usually called the execution context page.

  1. As a direct function calls, non-strict mode, this point window, strict mode, this point undefined;
  2. As a method of calling an object, this usually points to the object call.
  3. Use apply, call, bind you can bind point of this.
  4. In the constructor, this points to the newly created object
  5. Arrow functions no separate this value, determined in this function to create an arrow, with the same context in which it is declared in.

If a function repeatedly bind, then the context is what is it?

let a = {}
let fn = function () { console.log(this) }
fn.bind().bind(a)() // => ?

Whether we give this function bind several times, fn are always determined by the first bind, so the result is always a window.

// fn.bind().bind(a) 等于
let fn2 = function fn1() {
  return function() {
    return fn.apply()
  }.apply(a)
}
fn2()

When this rule appears more, this final point to where?

First of all, new ways the highest priority, followed by bind these functions, then obj.foo () calls this way, and finally foo called in a way, at the same time, this function of the arrow once bound, it is not longer be changed in any way.

Guess you like

Origin www.cnblogs.com/nayek/p/11729926.html