javascript數(shù)據(jù)類型示例分享
前面我們介紹了javascript的數(shù)據(jù)類型,今天我們通過一些例子再來溫故一下,希望大家能夠達到知新的地步。
<script type="text/javascript">
//1、Boolean 類型
//2、Number 類型
//3、String 類型
//Boolean類型容易與基本類型混淆,所以建議永遠不要使用Boolean對象。
//Number是與數(shù)字對應(yīng)的引用類型
var numberObj = new Number(10);
//重寫toString方法 傳入的參數(shù)是告訴它放回幾進制數(shù)字的字符串類型
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()方法,是返回指定小數(shù)位的數(shù)值的字符串表示方法,而且具有四舍五入的功能
var num = 10;
num.toFixed(2);//"10.00"
//toExponential()指數(shù)表示法方法,接受一個參數(shù)表示輸出結(jié)果中小數(shù)的位數(shù)
var num = 10;
alert(num.toExponential(1));//"1.0e+1"
//不過這么小的數(shù)字就不需要使用指數(shù)表示法了,如果你想得到某個數(shù)值最合適的格式就應(yīng)該使用
//toPrecision()方法,此方法可能返回固定大?。╢ixed)格式,也可能返回指數(shù)(exponential)格式
//接受一個參數(shù)表示數(shù)值所有數(shù)字的位數(shù)(不包括指數(shù)部分)。
var num = 99;
alert(num.toPrecision(1));//1e+2,1e+2表示100,因為指數(shù)無法表示99所以向上舍入變成100
alert(num.toPrecision(2));//"99"
alert(num.toPrecision(3));//"99.0"
//String對象,String對象的方法也可以在所有的基本字符串中訪問到。
//1、字符操作方法:charAt()、charCodeAt()。每個參數(shù)都接受一個基于位置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。
//這兩個方法的區(qū)別在于一個是從字符串的開頭向后搜索字符串,而lastIndexOf是從字符串的末尾向前搜索字符串。
//這兩個方法都有一個可選的參數(shù)(從指定的位置開始搜索)
var stringValue = "hello word";
alert(stringValue.indexOf("o"));//4
alert(stringValue.lastIndexOf("o"));//7
//可以循環(huán)調(diào)用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()這個方法會創(chuàng)建一個字符串副本,刪除前置及后置的所有空格。
var stringValue=" hello word ";
alert(stringValue);
alert(stringValue.trim());
//5、字符串大小寫轉(zhuǎn)換方法
//toLowerCase、toLocalLowerCase、toUpperCase、toLocalUpperCase
var stringValue="hello word";
alert(stringValue.toLocaleUpperCase());//此方法比較穩(wěn)妥
alert(stringValue.toUpperCase());
alert(stringValue.toLocaleLowerCase());//此方法比較穩(wěn)妥
alert(stringValue.toLowerCase());
//6、字符串匹配方法 replace()
//這個方法接受兩個參數(shù),第一個參數(shù)是一個正則表達式或者字符串,第二個參數(shù)是一個字符串或一個函數(shù)
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的第二個參數(shù)也可以是一個函數(shù)
function htmlEscape(text) {
//函數(shù)有是三個參數(shù):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)在字母表中排在字符串參數(shù)(B)之前,這返回負數(shù)(-1)
//如果字符串等于字符串參數(shù)則返回0
//如果字符串(A)在字母表中排在字符串參數(shù)(B)之后則返回正數(shù)(1)
var stringValue="f";
alert(stringValue.localeCompare("d"));//1
alert(stringValue.localeCompare("f"));//0
alert(stringValue.localeCompare("z"));//-1
//fromCharCode 這個靜態(tài)方法是與charCodeAt執(zhí)行相反的操作
alert(String.fromCharCode(104,101,108,108,111));//"hello"
//7、html方法建議不要使用。
</script>
END
童鞋們是否對javascript的數(shù)據(jù)類型有了新的認識了呢,希望大家能夠喜歡。
- javascript 簡單高效判斷數(shù)據(jù)類型 系列函數(shù) By shawl.qiu
- javascript 數(shù)據(jù)類型轉(zhuǎn)換(parseInt,parseFloat)
- javascript開發(fā)技術(shù)大全-第3章 js數(shù)據(jù)類型
- JavaScript基礎(chǔ)知識之?dāng)?shù)據(jù)類型
- JavaScript 學(xué)習(xí)筆記之?dāng)?shù)據(jù)類型
- Javascript基礎(chǔ)教程之?dāng)?shù)據(jù)類型 (字符串 String)
- Javascript基礎(chǔ)教程之?dāng)?shù)據(jù)類型 (數(shù)值 Number)
- Javascript基礎(chǔ)教程之?dāng)?shù)據(jù)類型 (布爾型 Boolean)
- Javascript基礎(chǔ)教程之?dāng)?shù)據(jù)類型轉(zhuǎn)換
相關(guān)文章
學(xué)習(xí)js在線html(富文本,所見即所得)編輯器
需要一個可以編輯同時又可顯效果的編輯框。textarea不行,它只能用來輸入純文本,不能顯示顏色、斜體之類的文字樣式,就像記事本,本文介紹所見即所得編輯器實現(xiàn)原理2012-12-12JavaScript基礎(chǔ)知識之?dāng)?shù)據(jù)類型
JavaScript中有5種簡單數(shù)據(jù)類型(也稱為基本數(shù)據(jù)類型):Undefined、Null、Boolean、Number和String。還有1種復(fù)雜數(shù)據(jù)類型——Object,Object本質(zhì)上是由一組無序的名值對組成的2012-08-08window.close(); 關(guān)閉瀏覽器窗口js代碼的總結(jié)介紹
下面小編就為大家?guī)硪黄獁indow.close(); 關(guān)閉瀏覽器窗口js代碼的總結(jié)介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07javascript基礎(chǔ)之查找元素的詳細介紹(訪問節(jié)點)
常用jQuery的話我們知道,jQuery有非常強大的選擇器來查找元素(也稱作訪問節(jié)點),例如:基本選擇器、層次選擇器、過濾選擇器、屬性選擇器等2013-07-07uniapp父子組件傳值3種方法(props、slot和ref)
這篇文章主要給大家介紹了關(guān)于uniapp父子組件傳值的3種方法,方法包括props、slot和ref,最近看到uniapp組件傳值的方法,這里記錄一下,需要的朋友可以參考下2023-07-07如何在標(biāo)題欄顯示框架內(nèi)頁面的標(biāo)題
如何在標(biāo)題欄顯示框架內(nèi)頁面的標(biāo)題...2007-02-02