JavaScript function definition

JavaScript uses the keyword function to define functions.

A function can be defined through a declaration or an expression.


Function declaration

In the previous tutorial, you already learned the syntax of function declaration:

function functionName(parameters) {
Executed code
}

The function will not be executed immediately after it is declared, but will be called when we need it.

Example

    php中文网(php.cn) 

本例调用的函数会执行一个计算,然后返回结果:

Run the program and try it


Tips: The semicolon is Used to separate executable JavaScript statements. Since the function declaration is not an executable statement, it does not end with a semicolon.


Function expression

JavaScript functions can be defined by an expression.

Function expressions can be stored in variables:

    php中文网(php.cn) 

函数可以存储在变量中:

Run the program to try it


After the function expression is stored in the variable, the variable can also be used as a function Use:

    php中文网(php.cn) 

函数存储在变量后,变量可作为函数使用:

Run the program to try it


The above function is actually an anonymous function (the function has no name).

Function is stored in a variable and does not require a function name. It is usually called through the variable name.

Tip: The above function ends with a semicolon because it is an execution statement.


Function() constructor

In the above example, we learned that functions are defined through the keyword function.

Functions can also be defined through the built-in JavaScript function constructor (Function()).

Example

    php中文网(php.cn) 

JavaScrip 内置构造函数。

Run the program to try it

Tips: In JavaScript, many times , you need to avoid using the new keyword.


Actually, you don't have to use a constructor. The above example can be written as:

    php中文网(php.cn) 

Run the program and try it


Function Hoisting (Hoisting)

We have already learned about "hoisting (hoisting)" in the previous tutorial.

Hoisting is JavaScript’s default behavior of hoisting the current scope to the front.

Hoisting is applied to variable declarations and function declarations.

Therefore, the function can be called before declaration:

myFunction(5);

function myFunction(y) {
return y * y;
}

Cannot be promoted when using an expression to define a function.


Self-calling functions

Function expressions can "self-call".

Self-calling expressions will be called automatically.

If the expression is followed by (), it will be called automatically.

The declared function cannot be called by itself.

By adding parentheses, it is shown that it is a function expression:

    php中文网(php.cn) 

函数可以自动调用:

Run the program to try it


The above function is actually an anonymous self-calling function (no function name).


Function can be used as a value

JavaScript function can be used as a value:

Example

    php中文网(php.cn) 

函数可作为一个值:

x = myFunction(4,3) 或 x = 12

两种情况下,x 的值都为 12。

Run the program and try it


JavaScript functions can be used as expressions:

    php中文网(php.cn) 

函数可作为一个表达式使用。

Run the program and try it


Function is an object

Use the typeof operator in JavaScript to determine the function type will return "function".

But it is more accurate to describe a JavaScript function as an object.

JavaScript functions have properties and methods.

arguments.length property returns the number of parameters received by the function calling process:

Example

    php中文网(php.cn) 

arguments.length 属性返回函数接收到参数的个数:

Run the program and try it



toString() method returns the function as a string:

    php中文网(php.cn) 

toString() 将函数作为一个字符串返回:

Run the program to try it

Tips: A function defined as an attribute of an object is called an object method. If the function is used to create a new object, it is called the constructor of the object.



Continuing Learning
||
php中文网(php.cn)

本例调用的函数会执行一个计算,然后返回结果:

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!