[toc]
## JS內置對象
>#### 什么是對象
1. 什么是對象:
JavaScript中的所有事物都是對象: 字符串、數值、數組、函數...
每個對象帶有屬性和方法
JavaScript允許自定義對象
```
people = new Object();
people.name = "xuxu";
```
2. 自定義對象
1). 定義并創建對象實例
```
<script>
people = {name: "xuxu", age: "22"};
document.write("name: "+people.name+" age: "+people.age);
</script>
```
2). 使用函數來定義對象, 然后創建新的對象實例
```
<script>
function people(name, age){
this._name = name;
this._age = age;
}
person = new people("xuxu", 22);
document.write(person._name + " : " + person._age)
</script>
```
>#### String對象
1. String對象:
String對象用于處理已有的字符串
字符串可以使用雙引號或單引號
2. 在字符串中查找字符串:indexOf()
```
var str = "Hello World";
document.write(str.indexOf("World"));
```
3. 內容匹配:match()
```
document.write(str.match("World"));
```
4. 內容替換:replace()
```
document.write(str.replace("World", "replace")); //hello replace
```
5. 字符串大小寫轉換:toUpperCase() / toLowerCase()
```
document.write(str.toUpperCase());
```
6. 字符串轉換為數組:strong>split()
```
var str1 = "hello, jike, xueyuan";
var s = str1.split(","); //以","分割數組
document.write(s[1]); //打印第二個數組. 或者其他分隔符都可
```
>#### Date日期對象
1. Date對象
日期對象用于處理日期和時間
```
var date = new Date();
document.write(date);
```
2. 獲得當日的日期
3. 常用方法
getFullYear(): 獲取年份
getTime(): 獲取毫秒
setFullYear(): 設置具體的日期(自己設置的時間)
```
ate.setFullYear(2010,1,1);
document.write(date); //Mon Feb 01 2010 20:45:03 GMT+0800 (中國標準時間)
```
getDay(): 獲取星期
```
/*顯示時間函數*/
<body onload="startTime()">
<p id="timetxt"></p>
<script>
function startTime(){
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
t = setTimeout(function(){startTime();}, 1000);
document.getElementById("timetxt").innerHTML = h+" : "+m+" : "+s;
}
/*在分秒前加"0"*/
function checkTime(i){
if(i<10){i="0"+i;}
return i;
}
</script>
</body>
```
>#### Array數組對象
1. Array對象:
使用單獨的變量名來存儲一系列的值
2. 數組的創建:
例:var myArray = ["hello", "iwen", "ime"];
3. 數組的訪問:
通過指定數組名以及索引號碼,你可以訪問特定的元素
注意:[0]是數組的第一個元素。[1]是數組的第二個元素。
4. 數組的常用方法:
concat(): 合并數組
```
var a = ["hello", "world"];
var b = ["iewn", "ime"];
var c = a.concat(b);
document.write(c); //hello,world,iewn,ime
```
sort():排序
```
var a = ["a", "c", "d", "t", "b", "e"];
document.write(a.sort()); //a,b,c,d,e 默認打印順序, 數字同理
```
```
/*降序排列*/
var a = ["5", "2", "4", "3", "1"];
document.write(a.sort(function(a, b){
return b-a; //升序改成a-b
}))
```
push():末尾追加元素
```
var a = ["a", "b"];
a.push("c"); //在末尾追加
document.write(a); //a,b,c
```
reverse():數組元素翻轉
```
var a = ["a", "b", "c"];
document.write(a.reverse()); //c,b,a
```
>#### math對象
1. Math對象
執行常見的算術任務
2. 常用方法:
round():四舍五入
```
document.write(Math.round(2.5)); //3
```
random():返回0-1之間的隨機數
```
document.write(Math.random()); //返回一個0-1之間的小數(小數點后14位)
document.write(parseInt(Math.random()*10)); //返回一個0-1之間的整型
```
max():返回最高值
```
document.write(Math.max(10, 20, 0.5, 1));
```
min():返回中的最低值
abs()
```
document.write(Math.abs(-10));
```
- H5筆記
- 1. Htm5與Html4的區別
- 2. Html5新增的主體結構元素
- 3. Html5新增的非主體結構元素
- 4. Html5表單新增元素與屬性
- JavaScript筆記
- 1.函數
- 2. 異常處理和事件處理
- 3. DOM對象
- 4. 事件詳解
- 5. 內置對象
- 6. DOM對象控制HTML元素詳解
- 7. 瀏覽器對象
- 8. 面向對象詳解
- jQuery筆記
- 1. jQuery簡介和語法
- 2. jQuery選擇器和事件
- 3. jQuery效果之隱藏與顯示、淡入淡出、滑動、回調
- 4. jQuery HTML之捕獲、設置、元素添加、元素刪除
- 5. jQuery CSS操作及jQuery的盒子模型
- 6. jQuery之遍歷與元素的過濾