Home > Web Front-end > JS Tutorial > A brief analysis of the difference between function declaration and function expression in javascript_javascript skills

A brief analysis of the difference between function declaration and function expression in javascript_javascript skills

WBOY
Release: 2016-05-16 16:13:53
Original
1006 people have browsed it

There are two ways to declare functions in JavaScript: function declaration and function expression.

The differences are as follows:

1). For functions defined by function declaration, the function name is required, while the function name of the function expression is optional.

2). For functions defined by function declaration, the function can be called before the function declaration, while the function of the function expression can only be called after the declaration.

3). Functions defined by function declaration are not real declarations. They can only appear globally or nested in other functions, but they cannot appear in loops, conditions or try/catch/ finally in, and

Function expressions can be declared anywhere.

The functions are defined in two ways below:

Copy code The code is as follows:

//Function declaration
function greeting(){
console.log("hello world");
}
//Function expression
var greeting = function(){
console.log("hello world");
}

An interesting javascript below:

Copy code The code is as follows:

function f() { console.log('I am outside!'); }
(function () {
if(false) {
// Repeat the declaration of function f
Function f() { console.log('I am inside!'); }
}
f();
}());

What will be output? The first reaction should be "I am outside". The result is "I am inside" in chrome, IE11 directly reports an error, and the lower version of firefox outputs "I am outside"...

The results output by chrome clearly reflect the characteristics of functions declared using function declarations-the function can be called before it is declared.

IE error shows that the object is missing because the function is declared in the condition, which violates the principle of function declaration.

Scope of function expression:

If the function declared by the function expression has a function name, then the function name is equivalent to a local variable of the function and can only be called inside the function. For example:

Copy code The code is as follows:

var f = function fact(x) {
If (x <= 1)
                       return 1;
                                                                                                                                                                                                                                                                                                                                     return x*fact(x-1);
                  };
// Uncaught ReferenceError: fact is not defined

fact() can be called inside the function, but when called outside the function, an error will be reported: fact is undefined


The above is the entire content of this article, I hope you all like it.

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