JavaScript function definition method instance details

The instance of this article describes the JavaScript function definition method. Share to everyone for reference, specifically as follows:

JavaScript function definition method

In the previous tutorial, you have already understood Syntax:

  {execution code}  

function declaration Immediately, it will be called when we need it.

Function myfunction (A, b) {RETURN A * B;}
Sequel is used to separate The JavaScript statement can be implemented.

Since the function declares is not an executable statement, it is not ended with the semicolon.

Function Expression

The JavaScript function can be defined by an expression. Function expression can be stored in variables:

VAR x = function (a, b) {RETURN A * B};

After the function expression is stored in the variable, the variable can also be used as a function:
VAR x = function (A, b) {RETURN A * B}; var z = x (4, 3);

The above functions are actually a

anonymous function
(function No name).
 Function StoreIn variables, no function name is required, usually calls through variables. The above functions ends in a semicolon because it is an execution statement.   Try to avoid using 
New keyword.


We have already understood “Hoisting” in previous tutorials.

Hoisting is the behavior that JavaScript upgrades the current scope to the previous behavior by default.

Hoisting Applications in Variable Declaration and Function of Function.

Therefore, the function can be called before the declaration:

MyFunction (5); function myfunction (y) {return y * y;}

The use of an expression definition function cannot be improved.

Motorless function

Functional expression can be “self-adjusted”.

The self-adjusted expression is automatically called.

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

Function that cannot be self-adjusted.
 By adding parentheses, it will be a function expression:   
(Function () {var x = “Hello !!”; // I will call myself}) ();

The above function is actually a function

anonymous self-call function (no function name).

The function can be used as a value

JavaScript function as a value:

Function myfunction (a, b) {RETURN A * B;} var x = myfunction (4, 3);

The JavaScript function can be used as an expression:
   Function myfunction (a, b) {RETURN A * B;} var x = myfunction (4, 3) * 2; 
Function is Object



operator in JavaScript, the function type will return “function” . However, the JavaScript function describes a more accurate as an object.

The JavaScript function has


method .
The number of parameters received by the function return process:

Function myfunction (a, b) {
method returns the function as a string:

Function myfunction (A, b) {RETURN A * B;} var txt = myfunction.tostring ();

moreMore about JavaScript Related Contents View this Topic: “JavaScript Common Function Skills Summary”, “JavaScript Object-Oriented Getting Counseling”, “JavaScript Error and Debugging Skill Summary”, “JavaScript Data Structure and Algorithm Skills Summary” and “JavaScript Mathematical Operation Usage summary

It is hoped that this article will help you JavaScript programming.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment