Understanding and thinking of call method in [front end] JS

Time:2019-12-18

Recently, I took over the front-end work, which caused a lot of confusion to the use of JS under the self-made JS framework in the current project. In particular, the relationship and transformation between JS classes, objects, functions, this and so on have been forgotten almost before, and now it is basically equivalent to a new look

The functions in JS can be interpreted in several ways: ordinary functions, classes, and an object. All three relationships can be regarded as coexistence

Function user() {}; // define a function
Console.log (user); // 1. This is a normal function
var u=new user();
Console.log (U); // 2. This is the user class
User. Tostring(); // 3. At this time, user is an object

 

 

Function is a function object or a class. All defined functions are function objects. User defined by me is also an object, and I can call methods in function
User.call (xxx): when I use user to call the call method, it is used as an object. It calls the call method in the function class. The function of this method is to call the function and override this in the function with the parameter passed in. The first parameter is to override this in the function, and the remaining parameter is passed in as the parameter of the function

function user(name){
Console. Log (‘I was called, this was overwritten ‘, this,’ parameter passed in ‘, name);
};
User. Call ({}, ‘Tao Shihan’);