Home >Web Front-end >Front-end Q&A >Can javascript functions be defined using var?
Javascript functions can be defined with var. In JavaScript, a function can be defined through a declaration or an expression, using the syntax "var funName = new Function (parameter list, "function structure statement");".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, functions can be declared and defined through the function keyword.
function funName([参数列表]) { statements }
You can also use the Function() constructor to define it through an expression:
var funName = new Function(参数列表,body);
The parameter types of Function() are all strings, and the body represents the function structure statement of the created function. , separated by semicolons between body statements.
Usage example:
You can omit all parameters and only pass a string to represent the function body.
var f = new Function ("a", "b", "return a+b"); //通过构造函数来克隆函数结构
In the above code, f is the name of the function created. The same function is defined, and the function keyword can be used to design a function with the same structure.
function f(a, b) { //使用function语句定义函数结构 return a + b; }
【Related recommendations: javascript learning tutorial】
The above is the detailed content of Can javascript functions be defined using var?. For more information, please follow other related articles on the PHP Chinese website!