The Declaration and invocation of JS method

Source: Internet
Author: User

Named function calls directly

function foo () {} foo ();

(2) Anonymous functions are invoked by reference.

Fooref = function () {}fooref ();

(3) anonymous function call without reference 1

(function () {} ());

(4) anonymous function call without reference 2

(function () {}) ();

(5) Anonymous function call without reference 3

void function () {
}();

Figure 1.1, figure 1.2 shows that the two expressions of the operation process is not the same, figure 1.1 is to use the force operator to perform the function call operation, figure 1.2 with the Force operator operation "function Direct Volume declaration" of the expression, and return a reference to the function itself, and then through the function call operation "()" To manipulate this function reference. The last anonymous function above calls the void function () {} (), which is used to call the function and ignores the return value, which is used to perform the function expression that follows it. If we do not use "void" and the Force Operation "()", the code can execute:

(1) function () {} ()//Use ' () ' to force call

(2) function () {} (); Use ";" To execute the statement

The scripting engine will assume that function () {} is a functional declaration, and thus pass the syntax detection, and the code is parsed like this:

function () {};();

function () {} is interpreted as a declaration, and "();" is interpreted as a separate line, so it will report a grammatical error and why it is known as "();" caused by the error? Let's change to the following code:

function () {} (1);

This will be interpreted by the engine as:

Fucntion () {};

(1); Single-Value expressions

Thus passed the grammar detection ...

    • Method invocation Pattern
    • Function call pattern
    • constructor invocation Pattern
    • Apply Call pattern

Let's take a look at some examples to understand better.

1: Method invocation mode.

Note This now points to MyObject.

/* Method Invocation Mode */

var myobject={

value:0,

Inc:function () {

Alert (This.value)

}

}

Myobject.inc ()

2: Function call pattern

Note this at this point, point to Window

/* Function call mode */

var add=function (A, b) {

Alert (this)//this is tied to window

return a+b;

} var sum=add (3,4);

Alert (SUM)

3: Constructor Invocation mode

The JavaScript language pristine book suggests abandoning this approach. Because there's a better way. This is not the first introduction. Post it the next time you post it.

Will add a connection here.

/* Constructor Call mode Discard */

var quo=function (String) {

this.status=string;

}

Quo.prototype.get_status=function () {

return this.status;

}

var qq=new Quo ("AAA");

Alert (Qq.get_status ());

The Declaration and invocation of JS method

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.