This is my second article about getting started

Bytedance has the following two questions to expand on

this

This represents a reference to the current object, which in JavaScript changes as the execution environment changes. In fact, this ends up referring to the pair that called it

In general, this refers to the following situations:

Used alone, this refers to the global object

In non-strict mode, this in a function refers to a global object

In strict mode, a simple call to the function, this is UND25th

Inside a function, the reference to this is undefined at function definition, only when the function is executed

In a method, this refers to the object on which the method is called

Apply, call, bind

Apply, call, and bind are methods to which the function’s this object points.

The apply() method calls a function with a specified this value and arguments supplied as an array (or array-like object).

fn.apply(obj, [1, 2]);

The first argument to the call method also refers to this, followed by a list of arguments. When a parameter is null or undefined, it points to the window (in the browser). Like Apply, call changes the this pointer only temporarily and executes it immediately.

fn.call(obj, 1, 2);

The bind method is similar to call in that the first argument points to this and is followed by a list of arguments (except that the list can be passed multiple times, whereas call must pass all arguments at once). However, it does not execute immediately after changing this. Instead, it returns a function that permanently changes what this refers to. fn.bind(obj, 1, 2);

Summarize apply, call and bind

All three are used to change the orientation of the function’s this object

In each case, the first argument is the object to which this refers, which is the context you want to specify

All three can be passed with subsequent parameters. Bind returns the corresponding function for later call; Apply and call are immediate calls.

Write apply, call, bind by hand

apply

call

bind