標籤:vue 記憶體 doctype str oct length 編碼 結束 dea
1.為了便於操作基本類型值,ECMAScript還提供了3個特殊的因喲用類型:Boolean、Number、Striung。
這些類型與其他參考型別相似,但同時也具有各自的基本類型相應的特殊行為,實際上每當讀取一個基本類型值得時候,
後台就會建立一個對應的基本封裝類型的對象,從而讓我們能夠調用一些方法來操作這些資料。
2.舉例示意。
<!DOCTYPE html><html lang="en">
<head> <title></title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="css/style.css" rel="stylesheet"> <script src="js/vue.js"></script></head>
<body> <!--基本封裝類型,每當讀取一個基本類型的時候,後台就會建立一個對應的基本封裝類型的對象--> <!--1.建立一個String執行個體,2.在執行個體上調用指定的方法3.銷毀這個執行個體。--> <script> // 使用new操作符建立的參考型別的執行個體在執行到離開範圍之前會一直儲存在記憶體中 var s1 = new String("some text"); //基底字元串轉化為對象, var s2 = s1.substring(2); console.log(s2);
// 而自動建立的基本包類型的對象在一行代碼執行瞬間然後就立即被銷毀 var s1 = "some text"; s1.color = "red"; console.log(s1.color); //underfined // 基本封裝類型執行個體調用typeof 會返回“”object,而且所有基本封裝類型都會轉換為布爾值true // 把字串傳給Object函數就會建立String的執行個體, // 而傳入的參數就會得到number的執行個體,傳入的布爾值參數就會得到Boolean的執行個體 var obj = new Object("some text"); console.log(obj instanceof String); //true
var value = "25"; var number = Number(value); //轉型函數 console.log(number); //number var obj = new Number(value); console.log(typeof obj); //object
// Boolean類型,其執行個體重寫了valueof()方法,返回基本類型值true falue // 不二運算式中所有的對象都會轉換為true var booleanObject = new Boolean(true); var falseObject = new Boolean(false); //false會被轉換為true var result = falseObject && true; console.log(result); //true
var falseValue = false; result = falseValue && true; console.log(result); //false
// Number類型 // Nunber類型也重寫了valueof(),toLocaleString()和toString()方法 // value()返回對象表示的基本類型的數值,另外兩個方法則返回字串形式的數值 var nunberObject = new Number(10); var num = 10; console.log(num.toString()); console.log(num.toString(2)); console.log(num.toString(8)); console.log(num.toString(10)); // 將數值格式化為字元轉的方法 var num = 10; console.log(num.toFixed(2)); //10.00,帶兩位小數 var num = 10.005; console.log(num.toFixed(2)); //10.01四捨五入
// Stirng類型 字串的對象封裝類型 var stringValue = "hello world"; console.log(stringValue.length); //11 //字元方法 charAt() charCodeAt() var stringValue = "hello world"; console.log(stringValue.charAt(1)); //e var stringValue = "hello world"; console.log(stringValue.charCodeAt(1)); //101小寫字母e的編碼 console.log(stringValue[1]); //e // 字串操作方法concat()字串拼接,concat()可以接受多個參數, var stringValue = "hello "; var result = stringValue.concat("world"); console.log(result); //hello world console.log(stringValue); //hello
// slice() substr() substring(),接受一個或兩個參數,第一個開始位置第二個結束位置。 var stringValue = "hello world"; console.log(stringValue.slice(-3)); //rld // substring()會將負值轉化為0 console.log(stringValue.substring(-3)); // 字串位置方法indexOf(),lastIndexOf(),方向相反 // 空格也算一個字元 var stringValue = "hello world"; console.log(stringValue.indexOf("h")); //從0開始 console.log(stringValue.indexOf("o")); //4 console.log(stringValue.lastIndexOf("o")); //7
var stringValue = "hello world"; console.log(stringValue.indexOf("o", 6)); //7 console.log(stringValue.lastIndexOf("o", 6)); //4
var stringValue = "Lorem ipsum dolor sit amet,consectetur adipisicing slit"; var positions = new Array(); var pos = stringValue.indexOf("e"); while (pos > -1) { positions.push(pos); pos = stringValue.indexOf("e", pos + 1);
} console.log(positions); //trim()方法,刪除一個字串的副本前後所有的空格 var stringValue = " hello world "; var trimmedStringValue = stringValue.trim(); console.log(stringValue); //" hello world "; console.log(trimmedStringValue); //hello world
//字串大小轉換方法 var stringValue = "hello world"; console.log(stringValue.toLocaleUpperCase()); //HELLO WORLD console.log(stringValue.toUpperCase()); //HELLO WORLD console.log(stringValue.toLocaleLowerCase()); //hello world console.log(stringValue.toLowerCase()); //hello world
// 字串模式比對方法 // match()方法接收一個參數要麼是一個參數要麼是個Regex // match()方法返回一個數組 var text = "cat, bat, sat, fat"; var pattern = /.at/; var matches = text.match(pattern); console.log(matches.index); //0 console.log(matches[0]); //cat console.log(pattern.lastIndex); //0 //search()方法 唯一參數與match()相同 var text = "cat, bat, sat, fat"; var pos = text.search(/at/); console.log(pos); //1 at第一次出現的位置。
// replace()方法,接收兩個函數,第一個參數可以正則對象也可以是字串, // 第二個函數可以是字串也可以是一個函數 var text = "cat, bat, sat, fat"; var result = text.replace("at", "ond"); console.log(result); //cond, bat, sat, fat替換, result = text.replace(/at/g, "ond"); //全域替換 console.log(result); //cond, bond, sond, fond var text = "cat, bat, sat, fat"; result = text.replace(/(.at)/g, "word($1)"); console.log(result); //word(cat), word(bat), word(sat), word(fat) //split();將字元分割成字串並放在一個數組中 var colorText = "red,blue,green,yellow"; var colors1 = colorText.split(","); var colors2 = colorText.split(",", 2); var colors3 = colorText.split(/[^\,]+/); console.log(colors1); //["red", "blue", "green", "yellow"] console.log(colors2); //["red", "blue"] console.log(colors3); //["", ",", ",", ",", ""]
//localeCompare()方法,這個方法比較兩個字串,並返回結果 //1.字串在字母表中應該排在字元產參數之前,返回負數 // 2.字串等於字串參數,返回 // 3.如果字串在字母表中應該排在字串參數之後,返回正數 var stringValue = "yellow"; console.log(stringValue.localeCompare("brick")); //1 console.log(stringValue.localeCompare("yellow")); //0 console.log(stringValue.localeCompare("zoo")); //-1 </script></body>
</html>
javascript基本封裝類型及其操作方法