The essence of javascript Language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.