JavaScript內置對象--基本包裝類型(Boolean、Number、String)詳解

一、什麼是基本包裝類型?

爲了便於操作基本類型值,ECMAScript還提供了3個特殊的引用類型: Boolean、Number、String。這些類型與其他內置對象類型相似,但同時具有各自的基本類型相應的特殊行爲。實際上,每當讀取一個基本類型值得時候,後臺就會創建一個對應的基本包裝類型的對象,從而讓我們能夠調用一些方法來操作這些數據。
包裝類型,是一個專門封裝原始類型的值,並提供對原始類型的值執行操作的API對象

二、其他內置對象與基本包裝類型對象的區別?

普通的內置對象與基本包裝類型的主要區別就是對象的生命期使用new操作符創建的引用類型的實例,在執行流離開當前作用域之前都一直保存在內存中,而自動創建的基本包裝類型的對象,則只是存在於一行代碼的執行瞬間,然後立即被立即銷燬。這意味着我們不能再運行時爲基本包裝類型值添加屬性和方法。

var s1 = "some text";
s1.color = "red";
alert(s1.color) ;  //undefined


在第二行爲s1添加一個color屬性,第三行代碼執行時,再次訪問s1,結果s1的color屬性被銷燬了

Boolean類型

Boolean類型是與布爾值對應的引用類型,要創建Boolean對象,可以像下面這樣調用Boolean構造函數並傳入true或者false值。




其實,Boolean對象在實際開發中用處不大,因爲它很容易造成人們的誤解。其中最常見的問題就是布爾表達式中使用Boolean對象。

例:
var a= new Boolean(false);
var b = a && true;
console.log(b);    //true
b = a && true;
console.log(b);  //false

案例中,很多人覺得第三行代碼是false,但實際是true,因爲在布爾表達式中所有對象都會轉爲true,因此a在布爾表達式中代表true,true && true當然結果是true,第四行代碼就是進行普通的邏輯運算了,返回false



Number類型

Number類型是數字對應的引用類型,要創建Number對象,可以在調用Number構造函數時向其中傳遞相應的數值。



Number類型的幾個方法:
1.toFixed():把數字轉換爲定點表示法表示的字符串,並具有指定的小數位數
注意:toFixed()方法只可以表示帶有0~20個小數位數的數值。

var num=10;
console.log(num.toFixed(2));  //10.00

var num2=10.005;
console.log(num.toFixed(2));  //10.01

2.toExponential(): 把數字轉換爲指數計數法表示的字符串,並具有指定的小數位數

var num=10;
console.log(num.toExponential(1));   //1.0e+1;


3.toPrecision():把數字格式化爲具有指定有效位的字符串

var num=99;
console.log(num.toPrecision(1));  //1e+2
console.log(num.toPrecision(2));  //99
console.log(num.toPrecision(3));  //99.0

4.toString():把數字轉換爲指定進制(默認十進制)表示的字符串

var num=10;
console.log(num.toString(2));  //轉換爲2進制,結果爲:1010





String類型

一、字符方法
1. str.charAt()  :以單字符串形式返回特定位置的那個字符

var stringValue = "hello world";
console.log(stringValue.charAt(1));   //e

2. str.charCodeAt() : 返回特定位置的字符編碼

var stringValue = "hello world";
console.log(stringValue.charCodeAt(1));  //101

二、字符操作方法
1. str.concat() : 用於將一個或多個字符串拼接起來,返回拼接得到的新字符串

var stringValue= "hello";
var result=stringValue.concat("world");
console.log(result);   //hello world


2. str.slice()/str.substring()/str.substr() : 這個三個方法都返回操作字符串的一個子字符串,而且都接受一或兩個參數,第一個參數指定子字符串開始的位置,str.slice()與str.substring()第二個參數表示子字符串到哪裏結束,如果第二個參數省略,則默認是結尾結束,str.substr() 第二個參數表示返回的字符個數

var stringValue = "hello world";
console.log(stringValue.slice(3));    //lo world
console.log(stringValue.substring(3));   //lo world
console.log(stringValue.substr(3));   //lo world
console.log(stringValue.slice(3,7));  //lo w
console.log(stringValue.substring(3,7));  //lo w
console.log(stringValue.substr(3,7));  //lo worl    返回字符個數7個


三、字符串位置方法
1. indexOf()/lastIndexOf() : 從一個字符串中搜索給定的子字符串,然後返回子字符串的位置(如果沒有找到,返回-1),這個兩個方法的區別在於: indexOf()方法從字符串的開頭向後搜索子字符串,而lastIndexOf()方法是從字符串的末尾向前搜索子字符串,然後這兩個方法可以接受第二個參數,表示從字符串中的哪個位置開始搜索。

var  stringValue = "hello world";
console.log(stringValue.indexOf("o"));   //4
console.log(stringValue.lastIndexOf("o"));   //7
console.log(stringValue.indexOf("o",6));   //7
console.log(stringValue.lastIndexOf("o",6));   //7


四、trim()方法
1. str.trim() : 創建一個字符串的副本,刪除前置及後綴的所有空格,然後返回結果

var stringValue = "   hello  world     ";
var trimString = stringValue.trim();  
console.log(stringValue);   //"   hello  world     "
console.log(trimString);     //"hello  world"


五、字符串大小寫轉換方法
1. str.toLocaleUpperCase() : 將所有大寫字母轉換爲小寫

var stringValue =  "HELLO WORLD";
var LocaleString=stringValue.toLocaleUpperCase();
console.log(stringValue);  //"HELLO WORLD"
console.log(LocaleString); //"hello world"


2. str.toUpperCase() : 將所有小寫字母轉換爲大寫

var stringValue = "hello world";
vae UpperString= stringValue.toUpperCase();
console.log(stringValue);  //"hello world"
console.log(UpperString);  //"HELLO WORLD"


六、字符串的模式匹配方法
1. str.search() : 這個方法用於檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串,由開頭向後查找,返回字符串中第一個匹配項的索引,如果沒有找到返回-1,這個方法專用於查找關鍵詞位置。

var stringValue="cat, bat, sat ,fat";
var searchString = stringValue.search(/.at/);
console.log(searchString); //1

2. str.match() : 這個方法可在字符串內檢索指定的值,或找到一個或多個與正則表達式匹配的字符,返回值是包含所有子字符串的數組,如果沒有找到,返回null嗎,這個方法專用於查找關鍵詞的內容。

相應的格式有:
str.match(/正則表達式/);  僅查找第一個出現關鍵詞的內容,區分大小寫
str.match(/正則表達式/i);   僅查找第一個出現關鍵詞的內容,不區分大小寫
str.march(/正則表達式/g);  查找所有關鍵詞的內容,區分大小寫
str.march(/正則表達式/ig);  查找所有關鍵詞的內容,不區分大小寫
注意:  i是忽略大小寫, g指的是全局

var stringValue="cAt, bat, sAt, fat";
var matchString1=stringValue.match(/.at/);
var matchString2=stringValue.match(/.at/i);
var matchString3=stringValue.match(/.at/g);
var matchString4=stringValue.match(/.at/ig);

console.log(matchString1);  //"bat"
console.log(matchString2);  //"cAt"
console.log(matchString3);  //"bat, fat"
console.log(matchString4);  //"cAt, bat, sAt, fat"


3.  str.replace() : 這個方法用於在字符串中用一些字符替換特定的字符,或替換一個與正則表達式匹配的字符,這個方法可以接受兩個參數,第一個參數是字符串或者是正則表達式,第二個參數可以是一個字符串或者一個函數,如果第一個參數是字符串,那麼只會替換第一個子字符串,如果第一個參數是正則表達式,則可以替換全部子字符串,前提是指定全局(g)標誌

var stringValue="cat, bat, sat, fat";
var replaceString1=stringValue.replace("at","ond");
var replaceString2=stringValue.replace(/at/g,"ond");
console.log(replaceString1);  //"cond, bat, sat ,fat"   第一個參數是字符串,所以只替換第一個子字符串
console.log(replaceString2);  //"cond, bond, sond, fond"   第一個參數是/正則表達式/g,  所有替換全部子字符串


七、localeCompare()方法
1.  str.localeCompare() : 這個方法用來比較兩個字符串,如果字符串在字母表中應該排在字符串參數之前,則返回-1,如果等於字符串參數,則返回0,如果排在字符串參數之後,則返回1,通常與參數首字母比較

var stringValue="yellow";
console.log(stringValue.localeCompare("zoo"));   //排在參數之前,返回-1
console.log(stringValue.localeCompare("year"));   //等於字符串參數,返回0
console.log(stringValue.localeCompare("brick"));   //排在參數之後,返回1


八、fromCharCode()方法
1. str.fromCharCode(): 這個方法是接收一個或多個字符編碼,然後將他們轉換成字符串,從本質上看,這個方法執行的是與charCodeAt()相反的操作

var stringValue = "104,101,108, 108 ,111";
console.log(String.(stringValue.fromCharCode));  //"hello"


九、HTML方法
早期的Web瀏覽器提供商差距了使用JS動態格式化HTML的需求,於是這些提供商就擴展了這些標準,實現了一些專門用於簡化常見HTML格式化任務的方法。不過,需要注意的是,儘量不要使用這些方法,因爲他們創建的標記通常無法表達語義

anchor(name)
big()
bold()
fixed()
fontcolor(color)
fontsize(size)
italics()
link(url)
small()
strike()
sub()
sup()

由於這些方法不經常使用,我就不一一列舉了,感興趣的同學可以自行學習。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章