內置核心對象
object string array date Math
一:對象
<script>
//對象的創建
//方法一:
var people=new Object();
alert (people); //[object Object]
alert (typeof(people)); //object
alert ( people instanceof Object); //true :相當於people是繼承與Object
//方法二:
var people =new Object;
alert (people); //[object Object]
alert (typeof(people)); //object
alert ( people instanceof Object); //true :相當於people是繼承與Object
//方法三:
var people ={
//鍵:值
name:"張三"
}
alert (people); //[object Object]
alert (typeof(people)); //object
alert ( people instanceof Object); //true :相當於people是繼承與Object
//方法四:
var people = function(){
}
//萬物皆對象
var boy= new people();
alert (people); // function(){}
alert (typeof(people)); //function
alert ( people instanceof Object); //true :相當於boy是繼承與Object
//對象的使用
//一:
var o=new Object;
o.name="shuang";
o["sex"]="man";
alert ( o.name +" "+ o.sex );//shuang man
//二
var o1={
name:'shuang',
sex:'man'
}
for( var i in o1){
alert (i+":"+o1[i]); //name :shuang sex: man
}
alert(o1.hasOwnProperty("name"));//true
</script>
二:數組
var arr=new Array(數組長度);
var arr=["","",""];
arr.sort();數組的排序
arr.join("-");鏈接
arr.push();在數組的最後一個位置添加,返回長度
arr.shift();在數組的第一個位置移除 返回該移除的元素
arr.pop();在數組的最後一個位置移除 返回該移除的元素
arr.unshift();在第一個位置添加;返回長度
Math.random();[0-1)
Math.floor( 數字 );//向下取整
Math.ceil( 數字 );//向上取整三
三:String
var str=new String()
var str="";
使用:
var str=document.title;
function line(){
str=str.substr(1,str.length)+str.substring(0,1);//substr:從1開始截取,截取str.length長度,不夠就截取到最後一位;
<span style="white-space:pre"> </span>//substring:從0開始截取截取到1,包括0不包括1【0.1)
document.title=str;
console.log(str);
}
window.setInterval("line()",500);
四:Math
Math.random() [0-1)
Math.floor( int ); 向下取整
Math.ceil( int ); 向上取整
Math.round( int ); 四捨五入
使用示例1:
<script>
var r,g,b;
function color(){
r=parseInt(Math.random()*256-1);
g=parseInt(Math.random()*256-1);
b=parseInt(Math.random()*256-1);
return "rgb("+r+","+g+","+b+")";
}
function RandomColor(){
document.getElementById("s1").style.color=color();
document.getElementById("s2").style.color=color();
document.getElementById("s3").style.color=color();
}
window.setInterval("RandomColor()",300);
</script>
五:date
var date=new Date();
date.getFullyear(); 獲取年份
date.getMonth(); 月份是從0開始的
date.getDate();
date.getDay(); 星期,從星期天開始 0
使用實例:
<script>
var arr=new Array(7);
arr[0]="星期天";
arr[1]="星期一";
arr[2]="星期二";
arr[3]="星期三";
arr[4]="星期四";
arr[5]="星期五";
arr[6]="星期六";
function date(){
var date=new Date();
year=date.getFullYear();
month=date.getMonth()+1;
dat=date.getDate();
day=date.getDay();
hour=date.getHours();
minu=date.getMinutes();
sec=date.getSeconds();
document.getElementById("a").innerHTML=year+"年"+month+"月"+dat+"日"+arr[day]+hour+":"+minu+":"+sec;
}
window.setInterval("date()",1000);//定時器,定時執行一個函數
</script>