Home > Web Front-end > JS Tutorial > body text

There are several ways to define functions in JavaScript

青灯夜游
Release: 2023-01-04 09:34:56
Original
4525 people have browsed it

There are three methods: 1. function keyword, syntax "function function name (parameter list) {//declaration}"; 2. Use function expression form "var variable name = function (parameter list) {//Declaration}" to define; 3. Use the "new Function()" constructor to define.

There are several ways to define functions in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Composition of function: function name function body

1. Use the function keyword to define the function--has priority Level, the function defined by the function keyword will be executed first

##

  function  functionName(arg0, arg1 ,..., argN){
      statements
  }
Copy after login

Function call: functionName()

2. Define the function in the form of a function expression (that is, copy the anonymous function to the variable)

   var  variable = function(arg0, arg1 ,..., argN){
    statements
   }
  console.log(typeof  variable);     //function
Copy after login

Function Call: variable();


3. Use the new Function constructor to define the function

  var  variable = new Function('name','alert("hello,"+name)');      //最末尾的是函数体,其前面的都是参数
  console.log(typeof  variable);     //function
Copy after login

Function call: variable('world');


Note:

(1) Use fucntion key For word-defined functions, once the function is declared, it is allowed to be called at any time (before the function is defined, after the function is defined, or inside the function, it can be called at any location)

(2) Use function expressions and new Function constructor definitions Functions cannot be used before the function is defined.

Parameters of the function:

Formal parameters: Parameters taken when the function is defined

Actual parameters: Parameters taken when the function is called

For more programming-related knowledge, please visit:

Programming Video! !

The above is the detailed content of There are several ways to define functions in JavaScript. 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!