JS基本封裝類型

來源:互聯網
上載者:User

標籤:操作方法   下標   參考型別   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基本封裝類型

聯繫我們

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