创建对象
people = new Object();
// people.name = "lx";
// people.age = "20";
// document.write("name:"+people.name+",age:"+people.age)
people = {name:"lx",age:"20"};
document.write("name:"+people.name+",age:"+people.age)
function people(name,age){
this.name = name;
this.age = age ;
}
son = new people("lx",20)
document.write("name:"+son.name+",age:"+son.age)
String对象
String用于处理已有的字符串
字符串可以使用单引号或双引号
在字符串中查找字符串:indexOf()
内容匹配:match() //有的话直接打印,没有的话返回null
内容替换:replace()
字符串大小写替换:
toUpperCase () / toLowerCase()
字符串转为数组:strong>split()
日期对象
时钟示例
<body onload="startTime()">
<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);
document.getElementById("timetext").innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function(){
startTime();
},1000);
}
function checkTime(i){
if (i<10){
i = "0"+i;
}
return i ;
}
</script>
<div id = "timetext"></div>
</body>
数组对象
数组合并
var a = ["曲肖冰"]
var b = ["当真","静悄悄"]
var c = a.concat(b)
document.write(c)
数组排序
var a = [1,3,4,5,7,8,0];
var b = ["a","c","e","d","b"]
document.write(a.sort());
document.write(b.sort());
倒序
document.write(a.sort(function(a,b){
return b-a;
}));
追加
var a =[1,2]
a.push(3);
document.write(a)
翻转
var a =[3,1,2,5,9]
document.write(a.reverse(a)
math对象