js中常見的內建對象

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">

function testDate(){
/**
* 測試Date對象
*/
var date = new Date();
console.log(date.getFullYear());
console.log(date.getYear());
console.log(date.getMonth());
console.log(date.getDay());
console.log(date.getDate());
console.log(date.getHours());
console.log(date.getMinutes());
console.log(date.getSeconds());
console.log(date.getMilliseconds());
}

function testString(){
var s = "this is a STRING test";
//穿件一個錨點 <a name="is">this is a string test</a>
//document.write(s.anchor("is"));
console.log(s.anchor("is"));
console.log(s.charAt(2));
console.log(s[2]);
console.log(s.charCodeAt(2));

console.log(s.concat(",this","is "));
//返回的是 第一個與它匹配的字串的位置
console.log(s.indexOf("is",8)) //第二個參數 表示 尋找的其實位置

console.log(s.replace("is",function (){
return "tttt"; //用函數處理 傳回值
}));

console.log(s.substr(1,2)); //長度 包含末尾
console.log(s.substring(1,2)); //位置 不包含末尾

console.log(s.toLowerCase());
console.log(s.toUpperCase());

//字串可以當作數組使用
s[2] = "tttt";
console.log(s);

for(var i in s){
console.log(s[i]);
}
}

function testMath(){
console.log(Math.abs("-10"));
console.log(Math.ceil(-1.2));
console.log(Math.floor(-1.2));

console.log(Math.max(1,2,.43,123,21,3,1,4,2,5,7,8));

console.log(Math.min(1,2,.43,123,21,3,1,4,2,5,7,8))

//在計算金融的時候 一定要適當的將大單位換成小單位計算
var n1 = 0.2-0.1;
var n2 = 0.3-0.2;
console.log(n1===n2);

}

</script>
</head>
<body>
<input type="button" onclick="testDate()" value="date">
<input type="button" onclick="testString()" value="string">
<input type="button" onclick="testMath()" value="math">
</body>
</html>

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.