4 calling modes of javascript functions

1. Function mode

// this points to the window global object

2. Method Pattern

// this points to the object that called this method

3. Constructor pattern

// this points to the newly created instance of new

4. Context Mode

//In the context call mode, the value of this can be modified, that is, the calling method of the function can be modified.

//Using the following two methods, you can modify the function call context, which is the value of this.

1. apply: function .apply(object, []); (the first parameter is the object to which this is modified).

2. call: function.call(object, arg1, arg2, arg3...).

// The difference between apply and call:

When a function requires parameters, apply uses an array to pass parameters, and call uses a single parameter to pass.

apply is used when the number of formal parameters of a function is uncertain,

call is used when the number of formal parameters of a function is determined.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325898158&siteId=291194637