變數提升題目:
(function(){ a=5 console.log(window.a) var a=10 console.log(a) })()
答案如下(還請先思考哦)
//編譯后 (function(){ var a; a=5; console.log(window.a) //undefined a=10; console.log(a) //10 })()
函式提升題目:
function test(){ console.log(1,foo); console.log(2,bar); var foo='hello' console.log(3,foo); var bar =function(){ return 'world' } function foo(){ return 'hello' } } test()
答案如下:
//編譯后
//函式提升優先級大于變數提升
function test(){ function foo(){ return 'hello' } var foo; var bar; console.log(1,foo); //foo(){ } console.log(2,bar); //undefined foo='hello' console.log(3,foo); //hello bar =function(){ return 'world' } } test()
總結:
1.對于用var宣告的變數,宣告會提升到其所在作用域的頂端,但賦值操作不會提升,
2.函式宣告同樣也會提升,這里僅限于函式宣告,并不包含函式運算式,
3.如果變數名和函式名一樣的話,函式提升優先級高于變數提升,
4.如果存在兩個函式宣告,則先出現的宣告先提升,后出現的宣告后提升,函式名相同的時候,后提升的會覆寫先提升的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/501013.html
標籤:其他
上一篇:day 8 Bom瀏覽器物件模型
