Home  >  Article  >  Web Front-end  >  What are JavaScript functions?

What are JavaScript functions?

青灯夜游
青灯夜游Original
2021-11-25 10:19:452726browse

In JavaScript, a function is a piece of encapsulated code that can be used (called) repeatedly. The data type is "function". JavaScript functions can assign values, operate, have properties and methods, and can even temporarily store values, construct instances, etc.; JS functions will be executed when a certain code calls it.

What are JavaScript functions?

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

In JavaScript, it is a piece of encapsulated code that can be used (called) repeatedly and belongs to "function" in the data type.

In JavaScript, a function can also be a value, an object, a type of data, or an expression. Therefore, functions can be assigned values, can operate, and can have properties and methods (so functions are also objects) , you can even temporarily store values, construct instances, etc.

How to create a function

Creating a function can be achieved through three methods: function declaration, function expression and constructor.

Function definition in Javascript A function declaration or function expression starts with the keyword function, followed by these components:

1. Function name identifier. The function name is a required part of the function declaration statement. Its purpose is like the name of a variable to which the newly defined function object will be assigned. For function definition expressions, this name is optional: if present, the name only exists in the function body and refers to the function object itself.

2. A pair of parentheses (). Contains a list of 0 or more comma-separated identifiers that are parameter names of the function, which act like local variables in the function body.

3. A pair of curly braces {}. It contains 0 or more JavaScript statements. These statements form the body of the function: they are executed once the function is called.

1. Function declaration

Define a function in JavaScript. The keyword function of the function is at the beginning of the entire statement block, and there is a function code with a function name identifier. It's called a function declaration.

Function declaration code example:

//函数声明示例
function fDeclaration(){
    
    console.log("这是函数声明示例");
    
}

2. Function expression

Function expression:

Define one in javascript Function, the entire function exists as a variable assignment statement or a call execution statement. The function statement block is called a function expression.

    //函数表达式示例
    var f = function fExpression() {
      console.log("这是函数表达式示例");
    }

    //这也是函数表达式,函数为匿名函数
    var fAnonymous = function () {
      console.log("这也是函数表达式示例");
    }

      //这还是函数表达式,函数为立即执行函数
      (function () {
        console.log("这还是函数表达式示例");
      }());

3. Constructor

The function called through the new keyword is called a constructor. Common constructor examples:

     var f=new Function("x","y","return x+y;");

This line of code creates a new function that is almost equivalent to the function defined by the following code:

     var f=function(x,y){
       return x+y;
     }

[Related recommendations: javascript learning tutorial

The above is the detailed content of What are JavaScript functions?. 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