Can javascript functions be defined using var?

青灯夜游
Release: 2022-02-16 14:58:00
Original
1610 people have browsed it

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
}
Copy after login

You can also use the Function() constructor to define it through an expression:

var funName = new Function(参数列表,body);
Copy after login

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");  //通过构造函数来克隆函数结构
Copy after login

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;
}
Copy after login

【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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!