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.