Home  >  Article  >  Web Front-end  >  Can javascript functions be defined using var?

Can javascript functions be defined using var?

青灯夜游
青灯夜游Original
2022-02-16 14:58:001583browse

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");".

Can javascript functions be defined using var?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn