JavaScript具体函数提升
JavaScript specific function hoisting
我想知道具体是什么函数在被吊起时最先出现。它们是否与开始时一样:
var z;
function b(){}
function a(){}
会变成
function b(){}
function a(){}
var z;
或者它还有别的作用吗?这对调用其他函数的函数有何影响?有
var z;
function b(){a();}
function a(){}
知道成为
function a(){}
function b(){a();}
var z;
还是我遗漏了什么?
吊装前
var z;
function b(){a();}
function a(){}
吊装后
reference to b;
reference to a;
z = undefined;
进入b
功能后,您将能够毫无问题地执行a
功能。
JS Engine performs following two steps to while executing any code:
CREATION PHASE:
- JS Engine parses - run through your code & identifies variables &
functions created by code (which will be used in execution phase)
- Setup memory space for Variables & Functions - "Hoisting"
- Hoisting - before your code is executed, the JS Engine set asides memory space
for Var & Func used inside the code. These variables & functions
comprise the Execution Context of any function that is be executed.
All variables in JS are initially set to undefined.
Execution PHASE:
pretty simple to understand,
- When the code is executed line-by-line (by JS interpreeter) it can
access the variables defined inside Execution Context
- variable
assignment are done in this phase
A new Execution Context is created whenever function invocation is there
Link 到 post - .
如MDN所述:
One of the advantages of JavaScript putting function declarations into
memory before it executes any code segment is that it allows you to
use a function before you declare it in your code.
所以.. 是的 - 您的函数在任何实际函数调用之前加载到内存中。例如:
function b() { console.log('b'); a() }
b()
function a() { console.log('a')}
将正确调用函数 a
,尽管 b
函数调用(第 2 行)位于 函数声明 之前 a
作为旁注..请尽量避免变量中的这种行为,因为您可能最终将变量附加到全局范围或使用 "scope bubbling" 的风险(使用此 link 了解更多详细信息)
函数和变量声明都被提升了,但是函数声明在变量之前被提升了。
所以你的代码:
var z;
function b(){}
function a(){}
被JS引擎编译后会变成:
function b(){}
function a(){}
var z;
现在的问题是下面的代码能正常工作吗?
var z;
function b(){a();}
function a(){}
在上面的代码中 function a
在声明之前在 function b
中被调用。 JS引擎分两个阶段工作,第一个是编译器阶段,所有提升都将发生,第二个是执行阶段,其中执行将发生。
因此,当您调用 function a
(最终将在执行阶段发生)时,将在编译阶段声明 function a
之后发生。
例如。
当你写:
console.log(a); // undefined cause var a; is declared first
var a = 10;
以上代码将被 JS 引擎解释为,
var a ;
console.log(a); // undefined;
a = 10;
的更多信息
我想知道具体是什么函数在被吊起时最先出现。它们是否与开始时一样:
var z;
function b(){}
function a(){}
会变成
function b(){}
function a(){}
var z;
或者它还有别的作用吗?这对调用其他函数的函数有何影响?有
var z;
function b(){a();}
function a(){}
知道成为
function a(){}
function b(){a();}
var z;
还是我遗漏了什么?
吊装前
var z;
function b(){a();}
function a(){}
吊装后
reference to b;
reference to a;
z = undefined;
进入b
功能后,您将能够毫无问题地执行a
功能。
JS Engine performs following two steps to while executing any code:
CREATION PHASE:
- JS Engine parses - run through your code & identifies variables & functions created by code (which will be used in execution phase)
- Setup memory space for Variables & Functions - "Hoisting"
- Hoisting - before your code is executed, the JS Engine set asides memory space for Var & Func used inside the code. These variables & functions comprise the Execution Context of any function that is be executed. All variables in JS are initially set to undefined.
Execution PHASE: pretty simple to understand,
- When the code is executed line-by-line (by JS interpreeter) it can access the variables defined inside Execution Context
- variable assignment are done in this phase
A new Execution Context is created whenever function invocation is there
Link 到 post - .
如MDN所述:
One of the advantages of JavaScript putting function declarations into memory before it executes any code segment is that it allows you to use a function before you declare it in your code.
所以.. 是的 - 您的函数在任何实际函数调用之前加载到内存中。例如:
function b() { console.log('b'); a() }
b()
function a() { console.log('a')}
将正确调用函数 a
,尽管 b
函数调用(第 2 行)位于 函数声明 之前 a
作为旁注..请尽量避免变量中的这种行为,因为您可能最终将变量附加到全局范围或使用 "scope bubbling" 的风险(使用此 link 了解更多详细信息)
函数和变量声明都被提升了,但是函数声明在变量之前被提升了。
所以你的代码:
var z;
function b(){}
function a(){}
被JS引擎编译后会变成:
function b(){}
function a(){}
var z;
现在的问题是下面的代码能正常工作吗?
var z;
function b(){a();}
function a(){}
在上面的代码中 function a
在声明之前在 function b
中被调用。 JS引擎分两个阶段工作,第一个是编译器阶段,所有提升都将发生,第二个是执行阶段,其中执行将发生。
因此,当您调用 function a
(最终将在执行阶段发生)时,将在编译阶段声明 function a
之后发生。
例如。
当你写:
console.log(a); // undefined cause var a; is declared first
var a = 10;
以上代码将被 JS 引擎解释为,
var a ;
console.log(a); // undefined;
a = 10;
的更多信息