javascipt-基礎隨筆

來源:互聯網
上載者:User

javascipt-基礎---細節需要注意的地方:

1、特殊數值:NaN、Infinity、isNaN()、isFinite()

NaN:

var a=parseInt('a123');window.alert(a); //輸出NaN

Infinity:

window.alert(6/0);//輸出Infinity 無窮大(最好不要這樣寫)

isNaN():判斷是不是數,不是數返回true,是數返回false

var a="dd";window.alert(isNaN(a)); //返回true

 isFinite():用於判斷是不是無窮大。如果 number 是 NaN(非數字),或者是正、負無窮大的數,則返回 false。

window.alert(isFinite(6/1)); //返回truewindow.alert(isFinite(6/0)); //返回false

 2、邏輯運算子:

在邏輯運算中,0、""、false、null、undefined、NaN均表示false

(或 || )||  將返回第一個不為false 那個值(對象亦可),或者是最後一個值(如果全部都是false的話)

這個知識點在javascript架構中運用很多。

a、

    var a=true;    var b=false;    var c=b || a;    window.alert(c); //輸出true

b、

    var a=2;    var b=0    var c= a || b;    window.alert(c); //返回第一個值,輸出2

c、

    var a=false;    var b="";    var c =0;    var d =new Object(); //對象    var aa=a || b || c ||d ; //a,b,c 全是false 這返回d    window.alert(aa); //返回d (對象) 

 4、多分支 switch

    var flag=1;    switch(flag){                default:        window.alert("啥都不是");                case 'a':        window.alert("a");        case 'b':        window.alert("b");  //沒有break語句,沒有匹配成功, 此時結果都輸出                }
    var flag=1;    switch(flag){                default:        window.alert("啥都不是");                case 'a':        window.alert("a");        case 1:        window.alert("b");  //沒有break語句 當匹配成功則不再找break語句 此時輸出b                }

 5、函數調用

func.js

function abc(val){        window.alert("abc()"+val);}//有傳回值的函數function test(num1,num2){    var res=0;    res =num1+num2;    return res;}//沒有傳回值的的函數function noVal(num1,num2){        var res=0;    res=num1+num2;}

函數調用:

<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><script type="text/javascript" src="func.js"></script><script type="text/javascript">        //函數調用1---普通調用(常用調用函數)    abc(50);    //函數調用2---變數=函數名; 調用的時候這樣調用:變數(實際參數)    var test1=abc;    //此時變數就相當於函數的引用(指標)    window.alert(abc);  //輸出abc整個函數代碼,就能理解了    test1(500);    //如果調用的函數有傳回值,可以在程式中直接返回,沒有傳回值但是你接收了,這是返回undefined    //調用有傳回值的函數    var res=test(20,40);    window.alert(res);         //調用沒有傳回值的函數    window.alert("調用沒有傳回值的函數");    var res=noVal(1,1);    //此時輸出undefined    window.alert(res);</script></head><body></body></html>

 js支援參數個數可變的函數 

<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><script type="text/javascript" src="func.js"></script><script type="text/javascript">    //函數調用--遞迴/*    function abc(num){                if(num>3){                        abc(--num);        }        document.writeln(num);    }        //調用函數    abc(5);   //輸出 3 3 4*/    //js支援參數個數可變的函數     function abc(){        //js中提供了一個arguments,可以訪問所以傳入的值        window.alert(arguments.length); //傳入多少個        //遍曆傳入的參數        for(var i=0;i<arguments.length;i++){                        window.alert(arguments[i]);        }    }        //調用    window.alert("abc(12,13,\"hello\",56)");    abc(12,13,"hello",56)        window.alert("abc(5)");    abc(5);    window.alert("abc()");    abc();</script></head><body></body></html>

 函數中使用全域變數與局部變數

<script type="text/javascript">    //全域變數與局部變數    var num=90;    function test(){                //在函數裡,如果不帶var就表示使用全域變數,        //如果帶var,表示在函數中定義一個新的 num變數        //var num=900;        num=900;    }    test();    window.alert("num="+num); //輸出num=900</script>

 6、訪問對象屬性有2中方式

<script type="text/javascript">/*  //js中 一切都是對象    function Person(){}    window.alert(Person.constructor);    var a=new Person();    window.alert(a.constructor);    window.alert(typeof a);    window.alert(Person);*/    //訪問對象屬性有2種方式 1、對象名.屬性名稱 2、對象名["屬性名稱"](動態調用)    function Person(){}        var a=new Person()    a.name="小明";    window.alert(a.name);//普通方式    var str="na"+"me";    window.alert(a[str]);//動態方式    //window.alert(a["name"]);//輸出小明</script>

 7、建立對象的方式有五種

  1. Factory 方法—使用new Object建立對象並添加相關屬性.
  2. 使用建構函式來定義類(原型對象).
  3. 使用prototype
  4. 建構函式及原型混合方式.
  5. 動態原型方式.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.