The essence of JavaScript language reading notes two

Source: Internet
Author: User

This section is a summary of the fourth chapter of language, and the essence of the comb, because this chapter is mainly to explain the function of this JS first-class citizens, so specifically to take out a section to hope to put this part to say through.

Fourth Chapter

Function object, the function object is also an object, and the function object creates a prototype property in the creation.

function literals , function objects created by function literals contain a connection to the external context, which is called a closure. This is the source of JS's powerful expressiveness.

function Call method , there are four ways to call the function, the four methods of invocation in the initialization of this key parameter has a difference:
1. Method invocation Mode
2. Function call mode
3. Constructor invocation mode
4. Apply Call mode

For 1, the method invocation pattern , simply say this object points to the caller, ex:

js code:    var x = {        value: 0,        call: function() {            console.log(this.value);        }    }    x.call();// 0,此时this指向x

For 2, the function call pattern for the function call pattern, this will be bound to the global object, the book said this is a design error, but I think it can be understood, when the function call pattern, It can be understood that the calling function is window.x, so in this mode point to Window, ex:

js code:    function x() {        console.log(this);    }    x();//window

For 3, the constructor pattern , in this mode, this points to the function instantiated by the new function, ex:

js code:    var X = function() {        this.zak = ‘wu‘;    }    X.prototype.getName = function() {        console.log(this.zak);    }    // 实例化    var x = new X();    x.getName();// ‘wu‘;

For 4, the Apply invocation pattern , in this mode, this is the binding object that is passed in at the time of the call, that is, the first parameter that is passed in by apply, ex:

js code:    var X = function() {        this.zak = ‘wu-X‘;    }    var y = {        zak:  ‘me-Y‘    }    X.prototype.getName = function() {        console.log(this.zak);    }    // 实例化    var x = new X();    x.getName.apply(y);// ‘me-Y‘;

function Arguments , which are parameters of a function that do not produce a run-time error when the number of arguments is not equal to the formal parameter. When the number of arguments is a long time, many of the parameters will be assigned to undefined, and when the actual parameter is limited, the non-passed parameter will be assigned the value of undefined. When the function is created, it will pass in arguments as the default property, arguments is a class array object, he has length, but there is no corresponding series of methods for the array. You can do this by [].slice.call(arguments) converting it to a real array.

function returns when the function executes to the return statement and returns undefined when there is no return statement, there is a special case where this is returned if the new prefix is prepended to the function call and the return is not an object.

Of course, this chapter is a lot of content, such as recursion, memory, modules and so on various coding schemes, or is best practice, writing is very good, worth reading.

For more information, please see Zakwu's small station.

The essence of JavaScript language reading notes two

Related Article

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.