javascript資料類型樣本分享_基礎知識

來源:互聯網
上載者:User

前面我們介紹了javascript的資料類型,今天我們通過一些例子再來溫故一下,希望大家能夠達到知新的地步。

複製代碼 代碼如下:

<script type="text/javascript">
        //1、Boolean 類型
        //2、Number 類型
        //3、String 類型
        //Boolean類型容易與基本類型混淆,所以建議永遠不要使用Boolean對象。
        //Number是與數字對應的參考型別
        var numberObj = new Number(10);
        //重寫toString方法 傳入的參數是告訴它放回幾進位數位字串類型
        var num = 10;
        alert(num.toString());//"10"
        alert(num.toString(2));//"1010"
        alert(num.toString(8));//"12"
        alert(num.toString(10));//"10"
        alert(num.toString(16));//"a"
        //toFixed()方法,是返回指定小數位的數值的字串表示方法,而且具有四捨五入的功能
        var num = 10;
        num.toFixed(2);//"10.00"
        //toExponential()指數標記法方法,接受一個參數表示輸出結果中小數的位元
        var num = 10;
        alert(num.toExponential(1));//"1.0e+1"
        //不過這麼小的數字就不需要使用指數標記法了,如果你想得到某個數值最合適的格式就應該使用
        //toPrecision()方法,此方法可能返回固定大小(fixed)格式,也可能返回指數(exponential)格式
        //接受一個參數表示數值所有數位位元(不包括指數部分)。
        var num = 99;
        alert(num.toPrecision(1));//1e+2,1e+2表示100,因為指數無法表示99所以向上舍入變成100
        alert(num.toPrecision(2));//"99"
        alert(num.toPrecision(3));//"99.0"
        //String對象,String對象的方法也可以在所有的基底字元串中訪問到。
        //1、字元操作方法:charAt()、charCodeAt()。每個參數都接受一個基於位置0的字元位置
        var stringValue = "Hello world!";
        stringValue.charAt(1);//"e" 第二個位置是“e”
        stringValue.charCodeAt(1);//"101" 第二個位置“e”的字元編碼是“101”
        //2、字串操作方法concat(拼接的字元)、slice(index,index)、substring(index,index)、substr(index,length)。index:位置,length:長度
        var str1 = "hello";
        alert(str1.concat(" word"));//Hello world
        alert(str1.concat(" word", "!"));//Hello world!
        var stringValue = "Hello world!";
        alert(stringValue.slice(3));//lo world
        alert(stringValue.substring(3));//lo world
        alert(stringValue.substr(3));//lo world
        alert(stringValue.slice(3, 7));//lo w
        alert(stringValue.substring(3, 7));//lo w
        alert(stringValue.substr(3, 7));//lo worl  這個7代表截取的長度
        //3、字串位置方法 indexOf() 和 lastIndexOf()
        //這兩個方法都是從指定的字串中搜尋給定的字串,然後返回字串的位置,沒有找到就返回-1。
        //這兩個方法的區別在於一個是從字串的開頭向後搜尋字串,而lastIndexOf是從字串的末尾向前搜尋字串。
        //這兩個方法都有一個可選的參數(從指定的位置開始搜尋)
        var stringValue = "hello word";
        alert(stringValue.indexOf("o"));//4
        alert(stringValue.lastIndexOf("o"));//7
        //可以迴圈調用indexOf或lastIndexOf來找到指定的字串
        var stringValue = "wo de wei lai bu shi meng!wo men you geng hao de ming tian!";
        var positions = [];
        var pos = stringValue.indexOf("e");
        while (pos > -1) {
            positions.push(pos);
            pos = stringValue.indexOf("e", pos + 1);
        }
        alert(positions);//4、7、22、33、38、47
        //4、trim()這個方法會建立一個字串副本,刪除前置及後置的所有空格。
        var stringValue="  hello word   ";
        alert(stringValue);
        alert(stringValue.trim());
        //5、字串大小寫轉換方法
        //toLowerCase、toLocalLowerCase、toUpperCase、toLocalUpperCase
        var stringValue="hello word";
        alert(stringValue.toLocaleUpperCase());//此方法比較穩妥
        alert(stringValue.toUpperCase());
        alert(stringValue.toLocaleLowerCase());//此方法比較穩妥
        alert(stringValue.toLowerCase());
        //6、字串匹配方法 replace()
        //這個方法接受兩個參數,第一個參數是一個Regex或者字串,第二個參數是一個字串或一個函數
        var text="cat,bat,sat,fat";
        var result=text.replace("at","ond");//
        alert(result);//"cond,bond,sond,fond"
        var result=text.replace(/at/g,"ond");//
        alert(result);//"cond,bond,sond,fond"
        var text="cat,bat,sat,fat";
        result=text.replace(/(.at)/g,"word ($1)");
        alert(result);
        //replace的第二個參數也可以是一個函數
        function htmlEscape(text) {
            //函數有是三個參數:1、模式比對項 2、模式比對項在字元中的位置 3、原始字串
            return text.replace(/[<>"&]/g,function(match,index,text){
                switch (match){
                    case "<":
                         return "<";
                    case ">":
                        return ">";
                    case "&":
                        return "&";
                    case "\"":
                        return """;
                }
            });
        }
        alert(htmlEscape("<p class=\"greeting\">Hello World!</p>"));
        //<p class="greeting">Hello World!</p>
        //localCompare()比較兩個字串。A.localCompare("B")
        //如果字串(A)在字母表中排在字串參數(B)之前,這返回負數(-1)
        //如果字串等於字串參數則返回0
        //如果字串(A)在字母表中排在字串參數(B)之後則返回正數(1)
        var stringValue="f";
        alert(stringValue.localeCompare("d"));//1
        alert(stringValue.localeCompare("f"));//0
        alert(stringValue.localeCompare("z"));//-1
        //fromCharCode 這個靜態方法是與charCodeAt執行相反的操作
        alert(String.fromCharCode(104,101,108,108,111));//"hello"
        //7、html方法建議不要使用。
    </script>

END

童鞋們是否對javascript的資料類型有了新的認識了呢,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.