看了幾道變量提高的小題想分享給你們,都很簡單,我這裏就不解釋緣由了。數組
咱們先說一下預編譯的過程函數
1.this
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(){} }
2.prototype
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(){} }
3.code
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
4.對象
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
5.繼承
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);
6.get
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);
7.原型
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);
8.io
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
9.
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
10.
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
其實上面一道題有關於變量提高可是不是重點由於還有原型、繼承、運算符優先級等知識點。
閒扯
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([] == ![]); //true 由於![] = false, false == []是成立的。 2 == [1] //true "" == [null] //true 0 == "/n" //true 由於'/n'被轉換爲0 ``` + [] + {}的問題 ``` console.log([] + {}); // "[object Object]" [] = "", "" + {} 調用{}的toString() console.log({} + []); // 0 只不過是轉換的先後順序不一樣 ```