Javascript learning notes function definition-javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces some basic knowledge about Function Definition in javascript, including Function declaration, Function expressions, and Function constructor. It is very simple and practical, if you have any need, refer. Function declarative

Function funname (parameter) {code executed}

The declarative function will not be executed immediately. It will be executed only after we call it: funname ();

* A semicolon is used to separate executable JavaScript statements. function declaration is not an executable statement, so it is not ended with a semicolon.

Function expression

Var x = function (parameter) {code block executed };

The function defined by the function expression is actually an anonymous function (this function has no name and is directly stored in the variable)

* The function expression ends with a plus sign because it is an execution statement.

Function Constructor

The Code is as follows:


Var myFunction = new Function ("a", "B", "return a * B ");


Call the function and assign it to a variable:

The Code is as follows:


Var x = myFunction (4, 3); // x = 12;

In actual production, it is not recommended to use constructors to define functions. The above example can be rewritten:

The Code is as follows:


Var myFunction = function (a, B) {return a * B };
Var x = myFunction (4, 3); // x = 12;

The above is all the content of this article. I hope you will like it.

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.