當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
javascript之预编译
生活随笔
收集整理的這篇文章主要介紹了
javascript之预编译
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先要記住:預編譯發生在函數執行的前一刻
先來一個小知識:
在預編譯階段,會把函數聲明提升到代碼頂部:
function test(){.... } test();在預編譯階段,會把var聲明提升到代碼段的頂端:
var a; console.log(a); a=123;控制臺輸出 undefined
但上面兩點在遇到下面這種情況時就不夠用了
function fn(a){console.log(a);var a=123;console.log(a);function a() {}console.log(a);var b=function () {}console.log(b)function d() {} } fn(1);接下來就是預編譯表演的時刻了:
原函數變為(忽略變量聲明與函數聲明):
function fn(a){console.log(a);//從AO中取a的值為function a() {}a=123;//把123賦給給AO中的aconsole.log(a);//此時a為123console.log(a);//還是123b=function () {}//把函數體賦給AO中的bconsole.log(b)//function b() {}function d() {} } fn(1);再來一個例子
function test(a,b){console.log(a);c=0;var c;a=3;b=2;console.log(b);function b() {}function d() {}console.log(b); } test(1);原函數變為(忽略變量聲明與函數聲明):
function test(a,b){console.log(a);//AO中a的值為1c=0;//0賦給AO中的ca=3;//3賦給AO中的ab=2;//2賦給AO中的bconsole.log(b);//AO中b的值為2console.log(b);//2 } test(1);練習:
function test(a,b){console.log(a);console.log(b);var b=234;console.log(b);a=123;console.log(a);function a() {}var a;b=345;var b=function b() {}console.log(a);console.log(b); } test(1); AO{a:function a() {},b:undefind }(忽略變量聲明與函數聲明)
function test(a,b){console.log(a);//function a() {}console.log(b);//undefindb=234;console.log(b);//234a=123;console.log(a);//123b=345;b=function b() {}console.log(a);//123console.log(b);//function b() {} } test(1);上面只是講述在函數中的預編譯,接下來加入全局的,中間插播一段作用域的小知識
回到我們的預編譯
var a=123; function a() {var a=b=3; } a();讓我們來熟悉一下全局對象
var a=100; function fn() {console.log(a); } fn();舉個栗子:
console.log(test); function test(test){console.log(test);var test=234;console.log(test);function test() {} } test(1); var test =123;以上為本人觀看成哥javascript視頻教程后對預編譯的總結,如有錯誤請指出。
轉載于:https://juejin.im/post/5b0f5edc6fb9a00a0c372c1b
總結
以上是生活随笔為你收集整理的javascript之预编译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Doc2Vec,Word2Vec文本相似
- 下一篇: Spring集成Mybatis plus