When javascript function declaration is promoted, will the function body be promoted together?
PHP中文网
PHP中文网 2017-05-19 10:43:15
0
10
773

javascript Function declaration promotion, will the function body be promoted together?

If is not , there are the following counterexamples:

console.log(demo.toString()); //‘function demo() {console.log('ok')}’

demo(); //‘ok’

function demo() {
    console.log('ok')
}

If is , there are the following counterexamples:

console.log(func)  // undefined   ,如果直接执行func()函数抛出TypeError异常 
if(true){
    function func() {
        console.log('11');
    };
}else{
    function func() {
        console.log('22');
    };
}
func(); // 11

The first example shows that the function body is also promoted. If you understand the second piece of code according to this idea, the "rename" functions will cover each other, and the later ones will cover the previous ones, so, console.log( func) will output the string 'function func() {console.log('22');};', but this is not actually the case.

Three questions arise:

  1. How do you understand "variable promotion, function priority"?

  2. Is the function body also improved?

  3. How to interpret the running result of the second code?

PHP中文网
PHP中文网

认证0级讲师

reply all(10)
PHPzhong

I believe you will understand everything after reading the article I wrote and have a deep understanding of the concepts of JavaScript execution context, function stack, and promotion

習慣沉默

if represents conditional judgment. The code should not be executed during compilation, but only syntax and lexical analysis will be checked. You can try changing the browser, or enable strict mode for testing. Unexpected things may happen~

1. Variable promotion, function priority, that is, var a = 1; function fun(){}; in var a; and function fun(){} will be promoted to the top level of the scope.
2. Also improved. The function expression will not, because it is var fun = function(){}; This kind will only improve var fun;
3. The second paragraph can actually be explained in the following way

var func;
console.log(func)
if(true){
    func=function func() {
        console.log('11');
    };
}else{
    func=function func() {
        console.log('22');
    };
}
func(); // 11

Peter_Zhu

For detailed introduction, see MDN http://kangax.github.io/nfe/#...

某草草

In non-strict mode, whether the function declaration placed inside the if statement will be promoted depends on the implementation of each kernel.

The conclusion is that the function will be improved, but it depends on the implementation of each core when inside the if statement.

Strict mode prohibits function declarations that are not at the script or function level

左手右手慢动作

You can refer to: /q/10...

洪涛

Function and variable declarations will be promoted. Function promotion takes precedence over variable promotion. Code to be run directly cannot be promoted. The first code is directly improved. The function you defined in the second code is in the if statement structure. The two log and if should be executed in order in the running stack. Obviously, your func has not been declared before the if statement is run.

刘奇

es6 has some block-level scope rules. If you run it with an old version of node or a lower version of chrome, it is equivalent to the following code

function func() {
   console.log('22');
};

console.log(func);
if(true){
}else{
}

func(); // 22
However, higher versions of node and chrome limit the improvement of such variables, which leads to the situation in your code! ~! ~

仅有的幸福

Because JavaScript is a function-level scope (function-level scope
所以if中并没有独立维护一个scope

English explanation:
javascript-variable-scope-and-hoisting-explained
Chinese explanation: Javascript scope and variable hoisting

洪涛

It is generally not recommended to write function declarations in conditional statements.
This is invalid syntax in ECMAScript, and the JavaScript engine will try to correct the error and convert it into a normal and reasonable state. However, this correction method is different in different browsers. It is recommended to use function expression:
var func;
if(true){

func=function {
    console.log('11');
};

}else{

func=function {
    console.log('22');
};

}

phpcn_u1582

http://blog.csdn.net/qq673318...
The link has an explanation
————Porter

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template