国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

弱類型語言javascript開發(fā)中的一些坑實例小結(jié)【變量、函數(shù)、數(shù)組、對象、作用域等】

2024-05-06 16:54:05
字體:
供稿:網(wǎng)友

本文實例講述了javascript/351996.html">弱類型語言javascript開發(fā)中的一些坑。分享給大家供大家參考,具體如下:

測試1: (未聲明變量自動提升為全局變量)

test1();function test1() {  function setName() {    name = '張三'; // 此處沒有var聲明,提升至全局  }  setName();  console.log(name);// '張三'}

測試2: (函數(shù)內(nèi)部局部變量的變量提升)

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是函數(shù)test3()里的b為2  }  hehe();}

測試4: (變量提升,局部作用域的綜合)

test4();function test4() {  c = 5;  function heihei() {    var c;    window.c = 3;    console.log(c); // 函數(shù)heihei內(nèi)的c為undefined    console.log(window.c); // 3  }  heihei();}

測試5: (數(shù)組的長度的問題)

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函數(shù)相關)

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: (變量提升,函數(shù)提升與return后阻斷執(zhí)行)

test9();function test9() {  function bar() {    return foo;    foo = 10;    function foo(){};  }  console.log(typeof bar()); // 'function'}

測試10: (作用域與函數(shù)提升)

test10();function test10() {  var foo = 1;  function bar() {    foo = 10;    console.log(typeof foo);    return;    function foo(){};  }  bar(); // number  console.log(foo); // 1}

測試11: (變量提升與函數(shù)提升)

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: (中間函數(shù)名,直接使用會報錯)

test13();function test13() {  var foo = function bar(name) {    console.log("hello " + name);  }  foo("world");  console.log(bar); // 此處會報錯 bar is not defined}

測試14: (在js中定時器,最后執(zhí)行,涉及到的知識點是事件循環(huán)和事件隊列)

test14();function test14() {  function foo() {    console.log('I am foo');  }  console.log('正常執(zhí)行');  setTimeout((function(){    console.log('定時器大灰狼來啦');  }),0);  foo();}

 

希望本文所述對大家JavaScript程序設計有所幫助。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南投县| 莒南县| 峡江县| 大埔县| 庄浪县| 永仁县| 肥乡县| 阿合奇县| 灵川县| 靖远县| 许昌市| 秭归县| 黄平县| 安顺市| 德阳市| 手游| 精河县| 八宿县| 岳西县| 中宁县| 崇阳县| 郴州市| 屏东市| 三明市| 巫溪县| 太谷县| 朝阳市| 唐河县| 三门县| 枝江市| 林西县| 西峡县| 濮阳市| 东明县| 莱西市| 石家庄市| 祥云县| 兴义市| 东方市| 晋城| 巴彦淖尔市|