以下Javascript执行顺序是否正确?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了以下Javascript执行顺序是否正确?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2828字,纯文字阅读大概需要5分钟。
内容图文
![以下Javascript执行顺序是否正确?](/upload/InfoBanner/zyjiaocheng/788/62d6ece2431044ebb57a6bbf4496d688.jpg)
编码noob试图理解JS.我有以下简单的代码,并试图详细分解正确的执行顺序.
function b() {
console.log(myVar)
}
function a() {
var myVar = 2;
b();
}
var myVar = 1;
a();
我试图将其分解如下
1. Global Creation Phase started where JS engine searches for all new variables and functions to be created..
First search for all new variables sitting on the Global stage
2. finds one variable called myVar
creates window.myVar memory space and assigns "undefined" as its value
window.myVar is NOT assigned 1 as its value at this stage!!!
finds no more new variables to be created
3. now searches for all new functions sitting on the Global stage to be allocated a memory space
finds two functions b( ) and a( ) to be created
4. it creates window.b( ) memory first since it lexically sits before a( )
4.1 new local creation phase started
searches for new variables
it finds no new variables to be created
4.2 it searches for new functions
it finds no new functions to be created
4.3 local execution phase started
creates memory space for console.log using window.myVar value that it found from the Global stage which is at this moment is "undefined"
it does not log the return value to the console yet....
finish window.b( ) memory creation phase
5. Start window.a( ) memory creation phase
5.1 all new creation phase started
searches for all new variables
finds var myVar command and creates a.myVar memory space and assigns "undefined" as its value
a.myVar is NOT assigned the value 2 at this stage !!!
5.2 searches for all new functions
finds no new function
5.3 local execution phase started
runs window.b( ) and stores the whatever the return value of window.b( ) into the return value of window.a( ) memory... which is to log "undefined" into the console
it still does not log its return value to console
finish window.a() memory creation phase
6. all local creation phase local execution phase and Global creation phase finished
7. Starts Global execution phase
8. Finds myVar = 1 command and assigns value 1 to window.myVar
9. finds a( ) command and execute function window.a( )
10. goes into window.a( ) function and assigns value 2 to a.myVar
finds b( ) command and executes window.b( ) passing window.myVar's value of 1 since window.myVar is what the window.b() return value is waiting for... not a.myVar value
receives the command to log 1 into console from function window.b( ) and sends it back to window.a( ) return value
11. back to global stage: window.a( ) function receives the return value which is: log 1 to the console
12. logs 1 to the console and finished.... phew!!!!
任何人都可以评论这个执行顺序是否正确…尤其是何时分配变量值…谢谢……
解决方法:
我不同意的两件事
>在2.之前,还有两个变量在全局级别定义了函数类型a和b.
>同样在4.和5.中,相应的本地创建阶段不会启动,直到它们被调用.
内容总结
以上是互联网集市为您收集整理的以下Javascript执行顺序是否正确?全部内容,希望文章能够帮你解决以下Javascript执行顺序是否正确?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。