Three methods for defining functions in JavaScript _ javascript skills

Source: Internet
Author: User
There are three methods to define a function in JavaScript: (1) declare an expression variable and define the expression of the variable. For example:

The Code is as follows:


Var func = function ()
{
/* Body code */
}


(2) define a function expression and specify its identifier. For example:

The Code is as follows:


Function func ()
{
// Body code
}


(3) use JavaScript built-in Function object construction. For example:

The Code is as follows:


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


Declaring a variable definition is different from using a function expression to identify a definition. We know that when a function is passed, it uses the reference transfer type, the variable definition is used to save the address reference of the expression, and the flag definition is used to save the address of the expression. Therefore, when we change or redefine a variable, the original expression does not change. When we change the identifier, the corresponding expression also changes. For example:

The Code is as follows:


// Declare a variable and define its expression reference
Var test = function ()
{
Alert ("reference test ");
}
// Define an expression and save its address information in test1
Function test1 ()
{
Alert ("reference test1 ");
}
// Pass the expression referenced by test to reference
Var reference = test;
// Pass the address of test1 expression to reference1
Var reference1 = test1;
// Change the reference of the variable test
Test = function ()
{
Alert ("new test ");
}
// Redefine the data in the test1 address
Function test1 ()
{
Alert ("new test1 ");
}
Alert (reference); // Its referenced expression does not change
Alert (reference1); // because reference1 is a reference to the test1 address, when the content indicated by the test1 address changes, the content of reference1 also changes

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.