The method of function declaration statement defines the method (function declarations Law) and expressions defined method (function expression)

First, the function definition:

In the description of what is to take a small example of the former function:

We all know that our country's four great inventions of printing (about science: China's four great inventions: papermaking, printing, gunpowder, the compass), one of the reason why there are printing, because of repeated copying a waste of time, efficiency and very low, so printing applications was born, the printing press is like a tool, with this tool, we do not need to repeat exactly to transcribe some of the content.

Said today's function is similar to printing, we found that at the time of writing the code, when implementing the same function, the code used is almost the same, because the actual need, so I had to repeat the same write code, then we write code that looks jumbled, take up memory space, resolution time will be very long, so, in order to make our code writing more beautiful, we can encapsulate our code, give it a name, place in a separate module, you need to use to take over to OK!

For example, when we do not use any direct loop you want to print five times "! Hello world", then we need to repeat five times the output "hello world!", The code is as follows:

document.write(('hello world!')+"</br>");
document.write(('hello world!')+"</br>");
document.write(('hello world!')+"</br>");
document.write(('hello world!')+"</br>");
document.write(('hello world!')+"</br>");

Next we package a function to print "hello world!"

HelloWorld function () { 
document.write (( '! Hello World') + "</ br>"); 
} 
HelloWorld () 
HelloWorld () 
HelloWorld () 
HelloWorld () 
HelloWorld () 
is obvious: We use the print function. " hello world ", do not have to repeat the same write code, just use the time to call on the OK!
Defined function: the function code requires repeated use, packaged as a separate module that called the function.

Second, the method function declaration:

Function defined by the function

We understand what the function is, then look at how to declare a function, popular point that how to use this function:

1. statement defines the method:

In fact, the way we use when printing "hello world" is the statement defines the law

Format: function name of the function () {

     Function block;

}

Call: function name ()

2. Expressions defined method:

One that expression, and is certainly related to the assignment, we can boldly guess what, the definition of law is an expression that we will not assign a function to a variable of it?

Look at its form:

var variable name = function [function name] () {

    Function block;

}

Call: variable name ()

Note: var variable name is behind it declares this variable, not a function name, function name or at the back function, the reason why the function name written in [], the law because the definition of the expression, the function name is omitted, under normal circumstances, the function name we are also omitted to write.

Statement defines the methods and expressions defined in the law after use, the statement defines the method will be more commonly used.

Why is it more common statement defines the law, which define the law say about the difference between statements and expressions defined in the Act:

3, method statements and expressions defined in the definition of the difference between the law:

Statement defines the law will enhance the function, the function will read the statement before executing the code, popular talk is what we can call this function in any position;

// Person () function call to the front seating // 
function Person () {
 var name = "John Doe" ; 
Alert (name); 
} 
Person () // after the function call // Zhang

Expression defines law will not improve the function, you must first declare the function, then call;

// Person (); error: Not IS A console function output Person 
var Person = function () {
 var name = "John Doe" ; 
Alert (name); 
} 
Person (); // Zhang

Function declaration operating rules:

Function declarations can only appear in a program or function in vivo. Syntax-wise, they do not appear in Block (Block) ({...}), for example not appear if, while or for statement. Since Block (Block) Statement only contain statements, declarations and source element can not contain such a function. On the other hand, a closer look will find the rules, so that the only possible expression situation occurs in the Block (block), it is to make it as part of an expression statement. However, the specification clearly defines the expression statement can not begin with the keyword function. This fact means that the function can not appear in the same expression statement Statement or Block (Block) (since Block (block) is composed of a statement Statement).

 

Following is a brief talk about the similarities and differences between function declarations and function expressions. Declarations and expressions of behavior there is a very subtle but very important difference.

  First, function declarations are parsed and first parsed and evaluated prior to evaluation in any expression . Even if the statement in the source code of the last line, it will be located in the same scope before the foremost expression is evaluated. Or to see an example easier to understand. In the following example, the function fn is declared after the alert. However, at the time of execution of the alert, fn have been defined:

alert(fn()); //输出Helloworld!  
 
function fn() {
return 'Helloworld!';
}
A brief summary of the difference in what place? 

1 . Declare always resolved first at the scope beginning;
 2 . Arithmetic expression only in the face of time. 

There is another important function declaration characteristics that control the function declaration by the behavior of conditional statements are not standardized, so it may get different results under different circumstances. That is:
// Do not do it! 
// different browsers have different return results, 
 
IF ( to true ) {
 function foo () {
 return 'First' ; 
} 
} 
the else {
 function foo () {
 return 'SECOND' ; 
} 
} 
foo (); 
 
 
// remember in this case to use the function expression: 
var foo;
 IF ( to true ) { 
foo = function () {
 return 'First' ; 
}; 
} 
the else { 
foo = function () {
return 'second';
};
}
foo();

Because of these restrictions, as long as the function in blocks (like the example above), in fact it should be viewed as a syntax error, rather than what the function declaration or expression.

  

  So we should use the function declarations or function expressions do at what time? Only appear in the function declaration "program code", this means that only the body of the function or other global space; they can not be defined or not assigned to a variable properties, or appear as a parameter in the function call; below examples are allowed to use a function declaration, foo (), bar () and local () is the function declaration by declaration mode:

// global environment 
function foo () {} 
 
function local () {
 // local environment 
    function bar () {}
         return bar; 
}

When you can not use the function declaration in syntax, you can use the function expression. For example: a transfer function defined as a parameter or function in a subject literals:

// This expression is an anonymous function 
callMe ( function () { 
 
// transfer function as an argument 
}); 
 
// this function is a named expression 
callMe ( function Me () { 
 
// pass a function as a parameter, a function named Me 
}); 
 
// other function expression 
var myObject = { 
    say: function () { 
 
// the I AM A expression The function 
} 
};

 

Guess you like

Origin www.cnblogs.com/itgezhu/p/11590109.html