Analysis of several definition methods of JS functions _ javascript skills

Source: Internet
Author: User
This article mainly introduces several defining methods of JavaScript functions. it analyzes the four defining methods of JavaScript functions and their specific usage skills, for more information about how to define JS functions, see the examples in this article. We will share this with you for your reference. The details are as follows:

The definition of JS functions is flexible. different from other languages, each function is maintained and run as an object.

Let's take a look at several common definitions:

Function func1 ([parameter]) {/* function body */}
Var func2 = function ([parameter]) {/* function body */};
Var func3 = function func4 ([parameter]) {/* function body */};
Var func5 = new Function ();

The first method is the most commonly used method.
The second is to assign an anonymous function to a variable. the call method is func2 ([function]).
The third is to assign func4 to the variable func3. the call method is func3 ([function]) or func4 ([function]).
The fourth is to declare func5 as an object.

Let's look at their differences:

Function func () {// function body} // equivalent to var func = function () {// function body}

But it is also a definition function, which has some differences in usage.

Script // This is the correct func (1); function func (a) {alert (a);} script

Script // This is incorrect. The system will prompt that func is undefined, mainly because func (1) is not defined before func is called; var func = function () {alert (a) ;}// this is correct. before calling func, you have defined var func = function (a) {alert (a);} func (1 ); script

The third definition method can be understood in the same way.

The fourth definition method is to declare an object before it can be referenced.

I hope this article will help you design JavaScript programs.

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.