The function definition of JavaScript learning notes

Source: Internet
Author: User
Tags constructor function definition

This article mainly introduces JavaScript some basic knowledge of function definition, including function declaration, function expression and function constructor, etc., very simple and practical, the need for small partners can refer to.

Function-declaration

?

1 2 3 4 5 function Funname (parameter) {... Executed code}

Declarative functions do not execute immediately, and require our invocation to execute: funname ();

* Semicolons are used to separate executable javascript statements, because function declarations are not an executable statement, so do not end with semicolons.

function expression

?

1 2 3 4 5 var x = function (parameter) {... code block to execute};

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

* A function expression ends with a semicolon, because it is an execution statement.

Function constructor

The code is as follows:

var myfunction = new Function ("A", "B", "return A * b");

Call a function and assign it to a variable:

The code is as follows:

var x = MyFunction (4, 3); x = 12;

It is not recommended to use constructors to define functions in actual production, and the above examples can be rewritten as:

The code is as follows:

var myfunction = function (a,b) {return a * b};

var x = MyFunction (4, 3); x = 12;

The above mentioned is the entire content of this article, I hope you can enjoy.

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.