本文實例講述了javascript/351996.html">弱類型語言javascript開發中的一些坑。分享給大家供大家參考,具體如下:
測試1: (未聲明變量自動提升為全局變量)
test1();function test1() { function setName() { name = '張三'; // 此處沒有var聲明,提升至全局 } setName(); console.log(name);// '張三'}
測試2: (函數內部局部變量的變量提升)
test2();function test2() { var a = 1; function haha() { console.log(a); var a=1; } haha(); // undefined}
測試3: (給window對象掛載屬性,作用域提升至全局)
test3();function test3() { var b=2; function hehe(){ window.b = 3; // 此時的b為全局變量的b console.log(b); // 此時的b是函數test3()里的b為2 } hehe();}
測試4: (變量提升,局部作用域的綜合)
test4();function test4() { c = 5; function heihei() { var c; window.c = 3; console.log(c); // 函數heihei內的c為undefined console.log(window.c); // 3 } heihei();}
測試5: (數組的長度的問題)
test5();function test5() { var arr = []; arr[0] = '1'; arr[1] = 'b'; arr[9] = 100; console.log(arr.length); // 10}
測試6: (等與全等的問題)
test6();function test6() { var a = 1; console.log(a++); // 1 console.log(++a); // 3 console.log(null == undefined); // true console.log(null === undefined);// false console.log(1 == "1"); // true console.log(1 === "1"); // false console.log(NaN === NaN) // false;}
測試7: (類型相關)
test7();function test7() { console.log(typeof 1); // number console.log(typeof "hello"); // string console.log(typeof typeof "hello"); // string console.log(typeof !!"hello"); // boolean console.log(typeof /[0-9]/); // object console.log(typeof {}); // object console.log(typeof null); // object console.log(typeof undefined); // undefined console.log(typeof [1, 2, 3]); // object console.log(toString.call([1, 2, 3])); // [object Array] console.log(typeof function () {}); // function}
測試8: (parse函數相關)
test8();function test8() { console.log(parseInt(3.14));// 3 console.log(parseFloat('3.01aaa'));// 3.01 console.log(parseInt('aa1.2'));// NaN; console.log(parseInt('8.00',16));// 8 console.log(parseInt('0x8',16));// 8 console.log(parseInt('8.00',10));// 8 console.log(parseInt('010',8));// 10 console.log(parseInt('1000',2));// 1000}
測試9: (變量提升,函數提升與return后阻斷執行)
test9();function test9() { function bar() { return foo; foo = 10; function foo(){}; } console.log(typeof bar()); // 'function'}
測試10: (作用域與函數提升)
test10();function test10() { var foo = 1; function bar() { foo = 10; console.log(typeof foo); return; function foo(){}; } bar(); // number console.log(foo); // 1}
測試11: (變量提升與函數提升)
test11();function test11() { console.log(typeof a); // function var a = 3; function a(){}; console.log(typeof a); // number}
測試12: (arguments對象)
test12();function test12() { function foo(a) { console.log(a);// 1 arguments[0] = 2; console.log(a);// 2 console.log(arguments.length);// 3 } foo(1,3,4);}
測試13: (中間函數名,直接使用會報錯)
test13();function test13() { var foo = function bar(name) { console.log("hello " + name); } foo("world"); console.log(bar); // 此處會報錯 bar is not defined}
測試14: (在js中定時器,最后執行,涉及到的知識點是事件循環和事件隊列)
test14();function test14() { function foo() { console.log('I am foo'); } console.log('正常執行'); setTimeout((function(){ console.log('定時器大灰狼來啦'); }),0); foo();}
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答