標籤:操作方法 下標 參考型別 new 替換 arc box 數字格式化 define
一.基本封裝類型概述
//是基本類型,但是又是特殊的參考型別。
var box = ‘Mr.Lee‘; //基本類型
alert(box.substring(2)); //對象.方法(參數),這種寫法明顯是參考型別的寫法
//substring索引從0開始,從第2個位置開始截取到末尾的字串
alert(‘Mr.Lee‘.substring(2)); //這樣的寫法也是可行的。
字面量寫法
var box = ‘Mr.Lee‘; //基本類型
box.name = ‘Lee‘; //給基本類型添加屬性
box.age = function()//給基本類型添加方法
{
return 100;
};
alert(box);
alert(typeof box);
alert(box.name); //undefined
alert(box.age()); //出錯
//基本類型是無法給自己建立屬性和方法的。
new運算子寫法
var box = new String(‘Mr.Lee‘); //String的參考型別
box.name = ‘Lee‘;
box.age = function()
{
return 100;
};
alert(box);
alert(typeof box); //Object
alert(box.name);
alert(box.age());
var box = 100;
alert(box.MAX_VALUE); //這種寫法叫做屬性
alert(Number.MAX_VALUE); //這種寫法(類型.屬性),叫做靜態屬性
var box = 1000.789;
//alert(typeof box.toString()); //將數值轉化為字串,並且可以轉換進位
//alert(box.toLocaleString()); //根據本地數字格式化轉換為字串
//alert(box.toFixed(2)) //小數點保留兩位,並轉換字串。
//alert(box.toExponential()); //以指數形式,並轉換字串。
//alert(box.toPrecision(8)); //根據傳參來決定指數或者點數
四.String類型
//String類型包含了三個屬性和大量的可用內建方法
var box = ‘Mr.Lee‘;
//alert(box.length); //返回字串的字元長度
//alert(box.constructor);//返回建立String對象的函數
//alert(box.prototype); //通過添加屬性和方法擴充字元串定義
字元方法
var box = ‘Mr. Lee‘;
//alert(box.charAt(1)); //返回指定下標的值
//alert(box.charCodeAt(4));//返回的是acssii碼
字串操作方法
var box = ‘Mr. Lee‘;
//alert(box.concat(‘ is‘,‘ Teacher‘,‘ !‘)); //字串串連
//alert(box.slice(4,6)); //返回字串n到m之間位置的字串
//alert(box.substring(4,6)); //返回字串n到m之間位置的字串
//alert(box.substr(4,6)); //返回字串n開始的第m個字串
//alert(box.slice(-2)); //7+(-2)=5,第5位開始,ee。
//alert(box.substring(-2)); //負數返回全部字串
//alert(box.substr(-2)); //7+(-2)=5,第5位開始,ee。
//alert(box.slice(2,-1)); //7+(-1)=6,(2,6);
//alert(box.slice(-2,-1)); //7+(-2)=5, 7+(-1)=6, (5,6)
//alert(box.substring(2,-1)); //參數如果是負數,直接返回0,(2,0)如果第二個參數比第一個小,那麼變成(0,2);
//alert(box.substr(2,-1)); //第二個參數為負數,直接返回0。(2.0)從第二個開始選擇0個,也就是空值。
字串位置方法
var box = ‘Mr. Lee is Lee‘;
//alert(box.indexOf(‘L‘)); //返回從初始位置搜尋L第一次出現的位置,4。
//alert(box.lastIndexOf(‘L‘)); //返回從末尾位置搜尋L第一次出現的位置,11。
//alert(box.indexOf(‘L‘,5)); //從第5個位置開始搜尋L第一次出現的位置,11。
//alert(box.lastIndexOf(‘L‘,5)); //從第5個位置開始向前搜尋L第一次出現的位置,4。
//alert(box.indexOf(‘,‘)); //找不到則返回-1
//樣本:找出全部的L
var boxarr = []; //存放L位置的數組
var pos = box.indexOf(‘L‘); //先獲得第一個L的位置,放進變數pos
while(pos > -1)
{
boxarr.push(pos); //添加到數組
pos = box.indexOf(‘L‘,pos+1); //重新賦值pos目前的位置
}
alert(boxarr);
大小寫轉換方法
var box = ‘Mr. Lee‘;
//alert(box.toLowerCase()); //全部轉換為小寫
//alert(box.toUpperCase()); //全部轉換為大寫
//alert(box.toLocaleLowerCase()); //全部轉換為小寫且本地化
//alert(box.toLocaleUpperCase()); //全部轉換為大寫且本地化
字串的模式比對方法
var box = ‘Mr. Lee‘;
//alert(box.match(‘L‘)); //找到字串則返回這個字串
//alert(box.match(‘,‘)); //沒有找到則返回null
//alert(box.search(‘L‘)); //找到L的位置。
//alert(box.replace(‘L‘,‘Q‘)); //替換掉找到的字串
//alert(box.split(‘ ‘)); //以空格分割成字串
其他方法
//alert(String.fromCharCode(76)); //找到asccii碼對應的字母
var box = ‘Lee‘;
//alert(box.localeCompare(‘Lee‘)); //1
//alert(box.localeCompare(‘Aee‘)); //0
//alert(box.localeCompare(‘Zee‘)); //-1
//alert(box.localeCompare(‘6576‘)); //1
//alert(box.localeCompare(‘是的‘)); //-1
HTML方法
var box = ‘Lee‘;
alert(box.link(‘http://www.baidu.com‘)); //link添加超連結方法
alert(box.bold()); //加粗方法
JS基本封裝類型