Three ways JavaScript defines a function summary _javascript tips

Source: Internet
Author: User
(1) Declare an expression variable and define the expression for that variable. Such as:
Copy Code code as follows:

var func = function ()
{
/*body code*/
}

(2) defines a function expression and specifies the identity of the expression. Such as:
Copy Code code as follows:

function func ()
{
Body Code
}

(3) Use JavaScript built-in function object constructs. Such as:
Copy Code code as follows:

var func = new Function ("/*parameters*/", "/*body code*/");

Declaring a variable definition differs from using a function expression identity definition. We know that the function uses the reference delivery type when it occurs, using the variable definition to save the address reference of the expression, and using the flag definition to save the address of the expression. So when we change or redefine a variable, it doesn't cause the original expression to change, and when we change the identity, the corresponding expression changes. Such as:
Copy Code code as follows:

Declare a variable and define an expression reference to the variable
var test = function ()
{
Alert ("Reference test");
}
Defines an expression that holds its address information in Test1
function Test1 ()
{
Alert ("Reference test1");
}
Pass the expression referenced by test to reference
var reference = test;
Passes the address of the test1 expression to the Reference1
var reference1 = test1;
Change the reference to the variable test
Test = function ()
{
Alert ("New test");
}
Redefining data in an test1 address
function Test1 ()
{
Alert ("New Test1");
}
alert (reference);//The expression to which it is referenced does not change
Alert (REFERENCE1)//Because Reference1 is a reference to test1 address, reference1 content changes as the contents of Test1 address change
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.