The essence of javascript Language
This section summarizes the essence of language in chapter 4, because this chapter mainly describes the functions of the first-class citizens in js, so I want to explain this part in detail.
Chapter 4
Function objectThe function object is also an object. The function object will generate a prototype attribute during creation.
Function literalThe function object created using the function literal contains a connection to the external context, which is called a closure. This is the source of js's powerful expressiveness.
Function call MethodThere are four call methods for a function. These four call methods differ in the key parameter "this" initialization:
1. method call mode
2. function call mode
3. constructor call mode
4. apply call mode
For1. method call modeTo put it simply, this object points to the caller, ex:
Js code: var x = {value: 0, call: function () {console. log (this. value) ;}} x. call (); // 0, this points to x
For2. function call modeIn the function call mode, this will be bound to a global object,This is a design mistake, but I think it can be understood in this way. In the function call mode, it can be understood that the called function is window. x, so in this mode, it points to window, Ex:
js code: function x() { console.log(this); } x();//window
For3. constructor ModeIn this mode, this points to the function after the new function is instantiated. ex:
Js code: var X = function () {this. zak = 'wu';} X. prototype. getName = function () {console. log (this. zak);} // instantiate var x = new X (); x. getName (); // 'wu ';
For4. apply call modeIn this mode, this will be the binding object passed in during the call, that is, the first parameter passed in by apply (call), ex:
Js code: var X = function () {this. zak = 'wu-x';} var y = {zak: 'Me-y'} X. prototype. getName = function () {console. log (this. zak);} // instantiate var x = new X (); x. getName. apply (y); // 'Me-y ';
Function ParametersWhen the number of real parameters is not equal to the value of the parameter, no running error is generated. When the number of arguments is large, multiple parameters are assigned an undefined value. When the number of arguments is small, unspecified parameters are assigned an undefined value. When a function is created, arguments is passed in as the default attribute. arguments is a class array object with length, but there is no corresponding array method. You can use[].slice.call(arguments)
Convert it to a real array.
Function returnWhen the function executes the return statement, it will return undefined if no return statement exists. Here is a special case. If the new prefix is added before the function call, if the returned result is not an object, this is returned.
Of course, this chapter contains a lot of content, such as recursion, memory, modules, and Other encoding schemes, or best practices. It is well written and worth reading.
For more information, see the zakwu website.