變數提升的考試題

codeXiu發表於2019-01-07

看了幾道變數提升的小題想分享給大家,都很簡單,我這裡就不解釋原因了。

  • 我們先說一下預編譯的過程
    1. 建立AO物件
    2. 找形參和變數宣告,放入AO中,值為undefined
    3. 形參實參相統一
    4. 找函式宣告,給AO中相應的的物件賦函式體
  • 下面我們來看幾個題:
console.log(bar());//11 列印的是函式的返回值
function bar(){
     foo = 100;
     function foo(){}
     var foo = 11;
     return foo;
}
1. AO {

}
2. AO {
    foo: undefined //變數宣告
}
3. AO {
    foo: undefined //無形參
}
4. AO {
    foo: function foo(){}
}
複製程式碼
function bar(){
     return foo;
     foo = 100;
     function foo(){}
     var foo = 11;
}
console.log(bar());//fun
1. AO {

}
2. AO {
    foo: undefined //變數宣告
}
3. AO {
    foo: undefined //無形參
}
4. AO {
    foo: function foo(){}
}
複製程式碼
function test(){
     console.log(b);//unde
     if(a){
         var b = 100;
     }
     c = 234;
     console.log(c);//234
}
var a;
test();
a = 10;
console.log(c);//234
console.log(global);//unde
global = 100;
console.log(global);//100
function fn(){
 console.log(global);//unde
 global = 200;
 console.log(global);//200
 var global = 300;
 console.log(global) //300
}
fn();
var global;
console.log(test);//fun
複製程式碼
 function test(test){
     console.log(test);//fun
     var test = 234;
     console.log(test);//234
     function test(){}
}
test(1);
var test = 123;
console.log(test);//123
複製程式碼
function test(a,b){
     console.log(a);//fun
     console.log(b);//unde
     var b = 234;
     console.log(b);//234
     a = 123;
     console.log(a);//123
     function a(){}
     var a;
     b = 28;
     var b = function(){}
     console.log(a);//123
     console.log(b);//fn
}
test(1);
複製程式碼
function test(a,b){
     console.log(a);//1
     c = 0;
     var c;
     a = 3;
     b = 2;
     console.log(b);//2
     function b(){}
     function d(){}
     console.log(b);//2
}
test(1);
複製程式碼
function fn(a){
     console.log(a);//fun
     var a = 123;
     console.log(a);//123
     function a(){}
     console.log(a);//123
     var b = function(){}
     console.log(b);//fun
     function d(){};
}
fn(1);
複製程式碼
console.log(a);//fun
a();//fun
var a = 3;
function a(){
    console.log(a);
}
console.log(a);//3
a = 6;
a();//error a 不是一個函式 a = 6
複製程式碼
a(3);
var a = 1;
function a(a) {
    console.log(a); //3
    console.log(b); //fun
    b = 1;
    c = 2;
    console.log(b); //1
    var b = 5;
    console.log(d); //fun
    function b(){};
    var a = 6;
    console.log(c); // 2
    if(false){
        var d = 1;
        c = 5;
    }
    console.log(c); //2
    function d(){};
    console.log(d); //fun
    arguments[0] = 7;
    console.log(a); //7
}
console.log(a); // 1
console.log(c); // 2
複製程式碼
function Foo(){
     getName = function(){
        alert(1);
     };
     return this;
}
Foo.getName = function(){alert(2);};
Foo.prototype.getName = function(){alert(3);};
var getName = function(){alert(4);};
function getName(){alert(5);};

Foo.getName();//2
getName();//4
Foo().getName();//1
new Foo.getName();//2
new Foo().getName();//3
new new Foo().getName();//3
複製程式碼

其實上面一道題有關於變數提升但是不是重點因為還有原型、繼承、運算子優先順序等知識點。

  • 閒扯

    • parseInt
    console.log(parseInt('a', 16)); //10
    console.log(parseInt(function(){}, 16)); //15 'f'
    console.log(parseInt(parseInt, 16)); //15 'f'
    console.log(parseInt(0.000002)); //0
    console.log(parseInt(0.0000002)); //2 '2e-7'  大於等於7位才會用科學計數法表示
    console.log(parseInt(false, 16)); //250 'fa'
    console.log(parseInt("113", 2)); //3 '11'
    複製程式碼
    • 立即執行函式的寫法
    (function a() {
        console.log(1);
    })();
    
    (function a() {
        console.log(1);
    }());
    
    +function a() {
        console.log(1);
    }();
    
    -function a() {
        console.log(1);
    }();
    
    !function a() {
        console.log(1);
    }();
    
    false || function a() {
        console.log(1);
    }();
    
    true && function a() {
        console.log(1);
    }();
    
    var a = function a() {
        console.log(1);
    }();
    複製程式碼

    上面的程式碼都相當於是立即執行函式,其實主要是你把它變成一個表示式它就能立即執行。也就是只有表示式才能被執行符號執行

    • 兩個陣列相加
    let a = [1, 2],
        b = [5, 3];
    console.log(a + b); //1,25,3  相當於呼叫了toString()
    複製程式碼
    • ==相等比較 我們都知道+0、-0、false、null、undefined、""、NaN都是假值。 我們來看幾個例子:
    console.log(a + b); //1,25,3
    console.log(false == ""); //true
    console.log(false == []); //true
    console.log(false == 0); //true
    console.log(false == null); //false
    console.log(false == undefined); //false
    console.log(false == NaN); //false
    console.log(false == {}); //false
    複製程式碼
    console.log("" == []); //true
    console.log("" == 0); //true
    console.log("" == null); //false
    console.log("" == undefined); //false
    console.log("" == NaN); //false
    console.log("" == {}); //false
    複製程式碼
    console.log(0 == ""); //true
    console.log(0 == []); //true
    console.log(0 == null); //false
    console.log(0 == undefined); //false
    console.log(0 == NaN); //false
    console.log(0 == {}); //false
    複製程式碼
    + [] + {}的問題
    console.log([] + {}); // "[object Object]"  [] = "", "" + {} 呼叫{}的toString()
    console.log({} + []); // 0 
    只不過是轉換的前後順序不同
    複製程式碼

相關文章