JS-對象的方法

來源:互聯網
上載者:User

標籤:fse   ring   cas   date()   正則   它的   字串串連   運算式   瀏覽器   

//String對象方法
// var str = ‘hello‘;
// console.log(str.length); //5
// console.log(str[0]); //h
//str中本身儲存的是字串類型資料,但是在使用時把對象轉換為偽對象
//所以能引用string對象的屬性
//偽對象:對於基本類型的資料,在讀取時,js會把資料轉換成相應的偽對象,再讀取偽對象(即
//讀出來是物件類型,因此有相應的屬性)。

//split 用於把一個字串分割成字串數組
// var str = ‘hello,world‘
// var result = str.split(‘l‘);
// console.log(result);

// var str = ‘123012301230456789‘
// var result = str.split(‘123‘);
// console.log(result);
// ["", "0", "0", "0456789"]

// var str = ‘123012301230456789‘
// var result = str.split(‘a‘);
// console.log(result);
//["123012301230456789"]

// var str = ‘123012301230456789‘
// var result = str.split(‘‘);
// console.log(result);
//["1", "2", "3", "0", "1", "2", "3", "0", "1", "2",
//"3", "0", "4", "5", "6", "7", "8", "9"]


//charCodeAt() 返回指定位置的字元的Unicode編碼,結果肯定是數字
// var str = ‘hello‘;
// console.log(str.charCodeAt(1)); //101

// //concat() 連接字串,把它的所有參數轉換成字串,
//然後按順序串連到字串 stringObject 的尾部,並返回串連後的字串
// 同Array用法 註:+也有相同的功能
// var str = ‘hello‘;
// var result = str.concat(‘js‘,‘haha‘)
// console.log(result);
// //hellojshaha
// console.log(str);
//hello

//indexOf() 返回某個字串片段在字串中首次出現的位置,從左至右
// var str = ‘hello‘;
// var result = str.indexOf(‘el‘);
// console.log(result); //1

// var str = ‘hello‘; //從左至右進行搜尋,搜尋的第一個l的角標
// var result = str.indexOf(‘l‘);
// console.log(result); //2

// var str = ‘hello‘; //沒有檢索到字串,傳回值為-1
// var result = str.indexOf(‘lll‘);
// console.log(result); //-1

// var str = ‘hello, world‘;
// var result = str.indexOf(‘l‘,6);
// //6代表從第幾位開始檢索
// console.log(result); //10

//lastIndexOf() 從右至左搜尋

//replace() 替換字串中的字元,只會替換第一個滿足的字元,
//如果全要替換,需要使用Regex
// var str = ‘hello, world‘;
// var result = str.replace(‘l‘,0);
// // l:想要替換的字元 0:要替換成什麼
// console.log(result);
// //he0lo,world
// console.log(str);
//hello,world


// slice() 截取片段,同Array
// var str = ‘hello, world‘;
// var result = str.slice(3,-1);
// console.log(result); //lo, worl

// toUpperCase() 轉換大寫
// var str = ‘helLo, worLd‘;
// console.log(str);
// //helLo, worLd
// var result = str.toUpperCase();
// console.log(result);
// //HELLO, WORLD
// console.log(str);
// helLo, worLd

//toLowerCase() 轉換小寫

// Number對象的方法 (萬物皆對象,使用時都是對象)
//toString() 任何一個對象都擁有 (js底層做的)
// var a = ‘123‘;
// var b = 100;
// var result = a + b;
// //1、把a和b轉換成相應的偽對象進行讀取
// //2、由於+號代表的是字串串連的操作符,此時會調用兩個偽對象的toString()
// //方法,把兩個方法的傳回值拼接在一起
// console.log(result); //123100

//valueOf() 將字串轉換為數字 ,任何一個對象都擁有 (js底層做的)
// var a = ‘123‘;
// var b = 100;
// var result = a - b;
// //—只有加減運算
// console.log(result); //23

//toFixed() 指定保留幾位小數,會四捨五入,結果為字串
// var num = 1.23456;
// console.log(num.toFixed(2)); //1.23
// console.log((1.2345).toFixed(2)); //1.23 方法二


//toExExponential() 把結果轉換為指數計數法,結果為字串
// var num = 123456.789;
// console.log(num.toExponential()); //1.23456789e+5

//toPrecision 指定指數計數法的指定位元
// var num = 123456.789;
// console.log(num.toPrecision(2)); //1.2e+5

// Math對象
// 屬性:
// PI:返回圓周率
// console.log(Math.PI); //3.141592653589793

// 方法:
// abs(x) 返回數的絕對值
// console.log(Math.abs(10)); //10
// console.log(Math.abs(-10)); //10

//min(x,y) max(x,y) 比起if判斷,低版本IE中效能好,
// console.log(Math.min(10,12)); //10
// console.log(Math.max(10,12)); //12

//ceil(x) 對數進行上舍入
// console.log(Math.ceil(10.01)); //11
// console.log(Math.ceil(10.00)); //10

//floor(x) 對數進行向下取整
// console.log(Math.floor(10.99)); //10
// console.log(Math.floor(10.00)); //10
//
// //round(x) 四捨五入
// console.log(Math.round(10.4)); //10
// console.log(Math.round(10.6)); //11

//random(x) 產生0-1之間隨意的小數
// var num = Math.random();
// console.log(num); //0.9598256822289126

//產生0-20之間隨意的數
// 先隨意產生0-1的小數,乘以21,再向下進行取整
// console.log(Math.floor(Math.random()*20));
// 產生1-20之間隨意的數
// 先隨意產生0-1的小數,乘以20,再向上進行取整

// Date對象 表示日期或時間
// var myDate = new Date(); 建立Date的方式,沒有字面量方式
// console.log(myDate); //Wed Oct 19 2016 10:54:59 GMT+0800 (中國標準時間)

// 方法:
// Date() 返回當天的日期和時間
// console.log(Date()); //Wed Oct 19 2016 10:56:44 GMT+0800 (中國標準時間)


//getDate() 返回是幾號
// var myDate = new Date();
// console.log(myDate.getDate());
//
// //getMonth() 返回時幾月 0為一月
// var myDate = new Date();
// console.log(myDate.getMonth());
//
// //getFullYear() 返回年份
// var myDate = new Date();
// console.log(myDate.getFullYear());
//
// //getDay() 返回星期幾 0為周日
// var myDate = new Date();
// console.log(myDate.getDay());

//getHours() 返回小時數 getMinutes/getseconds/getMilliseconds擷取毫秒數
//產生時間戳記,擷取唯一
//(低版本瀏覽器只能精確到10毫秒或30毫秒)
// var myDate = new Date();
// console.log(myDate.getHours());

//getTimezoneOffset() 擷取本地時間與標準時間的時間差(分鐘)
// var myDate = new Date();
// console.log(myDate.getTimezoneOffset());

//getUTCHours() 擷取標準時間的小時
// var myDate = new Date();
// console.log(myDate.getUTCHours());

//setHours() 設定幾點
// var myDate = new Date();
// myDate.setHours(10); //設定目前時間為10點
// console.log(myDate.getUTCHours());

//toString 把當前日期轉換為字串 ,格式為英文
// var myDate = new Date();
// console.log(myDate.toString());

//toLocaleString() 按照本地習慣顯示當前日期
// var myDate = new Date();
// console.log(myDate.toLocaleString());

//toTimeString() 只擷取時間
// var myDate = new Date();
// console.log(myDate.toTimeString());

//toLocaleTimeString() 按照本地習慣
// var myDate = new Date();
// console.log(myDate.toLocaleTimeString());

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.