javascript - Function declaration and declared variable exist at the same time, what are the rules for promotion?
高洛峰
高洛峰 2017-05-18 10:55:55
0
3
510

We know that declaring variables will be promoted:

var a = 1;
//上面一行相当于下面这样:
var a;
a = 1;

At the same time, function declarations will also be promoted:

foo();
function foo(){};
//上面两行相当于下面这样:
var foo;
foo = function (){};
foo()

So, when there are both variable and function declarations, who will be promoted to the higher level? How to sort? For example:

var a = 1;
foo();
function foo(){};

is that so?

var a;
var foo;
a = 1;
foo = function(){};
foo();

Because I saw this sentence in "JAVASCRIPT You Don't Know", I was a little confused:

Function declarations and variable declarations will be promoted. But a noteworthy detail is that functions are promoted first, then variables. Function declarations are hoisted before ordinary variables.

So it will be sorted like this:

var foo;
foo = function(){};
var a;
a = 1;
foo();

Solve the sorting rules, thank you!

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
仅有的幸福

This statement of "improvement first" is actually a misunderstanding. This is just a superficial appearance. The specifications are in no particular order (if you are interested in the implementation of V8, you can read here).

var 的提升是声明跟赋值分开,function Improvement is the whole improvement, so

var a = 1;
a();
function a(){};

It will become like this

var a;
function a(){};
a = 1;
a();

So there is the effect of the function being promoted first.

巴扎黑

First of all, we can divide it into 4 pieces of content

var a ;
a =1;
foo();
function foo();

We want to know what the sorting format looks like after promotion?
The current known condition is that variable declaration will be promoted to before variable assignment, so we can put the variable declaration in the first two digits first, regardless of its internal order for the time being:

var a ;
function foo(){};
a=1;
foo();

Next, let’s determine the internal ordering of declaration and assignment modules respectively.
In order to conduct experiments, we expanded the foo function, so the current order is:

var a;
function foo(){console.log(a+1)};
a=1;
foo();

Don’t worry about why we changed it like this. Let’s continue the experiment. We will understand it during the experiment.
In order to facilitate understanding, the experiment was conducted in two times.
First: we experiment with the variable declaration module.

var a=1;
console.log(foo);//若输出为function foo(){}则证明函数声明位于变量声明之前;若为undefined,说明相反。
foo();
function foo(){console.log(a+1)};

Then: we experiment with the variable assignment module.

var a=1;
foo();//若输出为undefined1,则证明foo()在a=1之前;若输出为2,则说明a=1在foo()之前。
function foo(){console.log(a+1)};

Let’s do the experiment:

var a=1;
console.log(foo);//若输出为function foo(){}则证明函数声明位于变量声明之前;若为undefined,说明相反。
foo();
function foo(){console.log(a+1)};

VM1099:2 function foo(){console.log(a+1)}
VM1099:4 2

Based on the experimental results, our final ranking result is:

function foo(){};
var a;
a=1;
foo();
黄舟
var a = 1;
foo();
function foo(){};
提升之后
function foo(){};
var a;
a = 1;
foo();

给你举个更明显的例子
console.log(1, foo, typeof foo);
var foo = "变量";
console.log(2, foo, typeof foo);
function foo(){
    console.log("函数声明");
}
console.log(3, foo, typeof foo);
=》
function foo(){
    console.log("函数声明");
}
var foo;
console.log(1, foo, typeof foo);
foo = "变量";
console.log(2, foo, typeof foo);
console.log(3, foo, typeof foo);
输出为:
1 function foo(){
    console.log("函数声明");
} "function"
2 "变量" "string"
3 "变量" "string"
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!