function call---js authoritative guide

Source: Internet
Author: User

Four methods of invocation:

    • As a function
    • As a method
    • As a constructor (to be continued)
    • Call () and apply () method calls through them (to be continued)

function call : Simply call the function as a normal function.

according to ES3 and non-strict ES5, the context (this) of a function call is a global object. In strict mode, the calling context is undefined. you'll see in the following code.

This can also be used to determine if it is a strict mode,

var strict = (function() {return !  this;} ());

Method Invocation (tonight's focus):

The method is simply a property of an object, but this property is a function. For example, in the following code: an object is defined, and M is an attribute of O, which is the method. Call: O.M ();

main extents of method calls and function calls do not: call context.

Method Chain: The method return value is an object, and the object can call its other methods again, such that the call sequence is called the method chain. For example, write in jquery:

$ (": Header"). Map (functionreturnthis. ID}). Get (). sort ();

First get all the headers, get an object, then give the object a map (map) (a function) to get their IDs, then convert those IDs into arrays and sort them.

The following code deals with scopes and this.

This does not have a scope limit, and the nested function does not inherit this from the function that called it, that is, in the following code, the eighth line of this is in a nested function, and the nested function is called as a function . So according to the preceding rule, the this of the function call is a global object or a undefined.

In order for a nested function to have access to the this value of an external function, it needs to be stored in a variable that is in the same scope, the third row, and the fifth row, usually with self to save this.

1 varo = {//define an object o2M:function(){//method of Object o m ()3             varSelf = This;//saves the This value of the M () method in the variable self4Console.log ( This= = = O);//True,this is the object o5f ();//call function f ()6 7             functionF () {//define a nested function8Console.log ( This= = = O);//False This is a global object or undefined9Console.log (self = = = O);//trueTen             }  One        } A     }; -O.M ()//method of calling object o m ()

It seems that you have encountered such a problem in the written examination.

For my English major, sometimes it takes some time to understand some concepts, or to look at some specific application examples of concepts to understand, but I am paranoid about the concept of understanding.

Formal parameter (parameter): A variable that is equivalent to a function definition. (as an example of a parameter passed in as an argument, a. B (examples of the following faces), which is the equivalent of Var A, c), when it is passed as a formal parameter, it has already been defined, that is, declared.

Argument (argument): The parameter passed in when a function call at run time.

Let's look at an example:

function F (A, B) {...}            // Parameter f (c,d) when the function is fixed ;                            // arguments passed in when the function is called, as opposed to A/b

2015-12-21 23:02:26 has A SWEET DREAM.

function call---js authoritative guide

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.