We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
类型:基础知识 难度:★
有以下 html 文件,请问运行后浏览器控制台输出结果是什么?为什么?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <script> function fn() { console.log(1); } fn(); function fn() { console.log(2); } fn(); </script> <script> function fn() { console.log(3); } fn(); </script> </body> </html>
参考答案:
控制台依次输出:2, 2, 3
2, 2, 3
原因:JavaScript 解释器在执行页面脚本时,是按块执行的。具体来讲,就是当浏览器在解析 HTML 文档时,如果遇到一个 script 标签,则 JavaScript 解释器会等到这个代码块都加载完成后(读取到当前代码块的 </script> 标签),先对代码块进行预编译,然后再执行。执行完毕后,浏览器会继续解析下面的 HTML 文档,同时JavaScript 解释器也准备好处理下一个代码块……
</script>
上面代码的执行步骤:
<script></script>
fn
fn()
2
3
题外话:如果 script 标签带有 defer、async 属性,情况有所不同。
defer
async
相关资料:https://www.jianshu.com/p/bda5266755a2
本期优秀回答者: @AMY-Y
The text was updated successfully, but these errors were encountered:
/*2 2 3 *原因: *第一个script标签内: * 函数声明 方式创建的函数,会存在函数声明提前,代码等同于 */ function fn() { console.log(1); } function fn() { console.log(2); }//相同函数名,在html内部,按顺序查找函数名,后面的会覆盖前面的 fn(); //2 fn(); //2 //最下面的没找到参考文章。自己猜测原因:还是HTML内部js按顺序查找函数名,后面的会覆盖前面的
参考文章: https://www.cnblogs.com/echolun/p/7612142.html https://www.cnblogs.com/wshiqtb/p/3480597.html
Sorry, something went wrong.
No branches or pull requests
有以下 html 文件,请问运行后浏览器控制台输出结果是什么?为什么?
参考答案:
控制台依次输出:
2, 2, 3
原因:JavaScript 解释器在执行页面脚本时,是按块执行的。具体来讲,就是当浏览器在解析 HTML 文档时,如果遇到一个 script 标签,则 JavaScript 解释器会等到这个代码块都加载完成后(读取到当前代码块的
</script>
标签),先对代码块进行预编译,然后再执行。执行完毕后,浏览器会继续解析下面的 HTML 文档,同时JavaScript 解释器也准备好处理下一个代码块……上面代码的执行步骤:
<script></script>
中的代码,下同);fn
函数,后面一个会覆盖前面一个函数的定义);fn()
,故依次输出2
,2
);3
题外话:如果 script 标签带有
defer
、async
属性,情况有所不同。相关资料:https://www.jianshu.com/p/bda5266755a2
本期优秀回答者: @AMY-Y
The text was updated successfully, but these errors were encountered: