Function expression in javascript

Source: Internet
Author: User

Function expression in javascript

Functions in JavaScript are actually equivalent to methods in java, but the format of method definition is different. However, java and JavaScript are completely different languages. If you don't think either of them has a java, you think they have a relationship. In fact, they have nothing to do with them. The following describes the function expression in JavaScript.

 

I. function declaration method

A. Basic syntax
Function keywords

1,
Function Name ([parameter 1], [parameter 2]...) {
Function body
[Retrun] // Return Value
}

B. literal definition form (anonymous function)


2,
Var variable = function ([parameter 1], [parameter 2]...) {
Function body
[Retrun] // Return Value
}

C. Declare it as an object


3,
Var variable = new Function ([parameter 1], [parameter 2]..., Function body );

 

I personally think that the first function declaration method is better because the first method is intuitive and the code is readable.

Ii. function call method:

A. Function Name () and variable name ();


B. (function () {alert (who I am );})();

 

Iii. Differences between the three declarative Methods


1. If the names of the two functions are the same, the subsequent functions will overwrite the previous functions.

2. Functions declared in basic syntax are loaded into the memory in advance during code execution for future use,
However, a function named literally is assigned a value only when it is executed.

3. When using and calling functions in different <script> </script> blocks, you should first define them and then execute them. It is like if you go to a restaurant to eat, you must first serve dishes, if you want to eat again, you cannot eat food first. It is not scientific.

 

 

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.