自定义对象:
1.定义并创建:
people =new Object();
people.name="iwen";
people.age =30;
document.write("name"+people.name+",age:"+people.age);
people1={name:"iwen",age:"age"};
document.write("name"+people1.name+",age:"+people1.age);
2.用函数创建:
function people(name,age){
this.name=name;
this.age=age;
}
son=new people("iwen",30);
document.write("name"+son.name+" age:"+son.age);
字符串对象:
length属性
var str="hello world";
document.write(str.length);
输出11 说明包括空格
indexOf查找是否有串:
var str="hello world";
document.write(str.indexOf("world"));
输出6 没有输出-1
match()是否匹配 匹配 输出串 不匹配 输出-1
var str="hello world";
document.write(str.match("world"));
输出world
var str="hello world";
document.write(str.match("world1"));
输出null replace:
var str="hello world";
document.write(str.replace("world","jike"));
输出 hello jike
toUpperCase toLowerCase
字符串转化为数组(常用)
var str1="hello jike xuyuan";
var s=str1.split(" ");
document.write(s[1]);
Date:
当前时间:
var date=new Date();
document.write(date);
输出: Thu Oct 29 2015 16:57:17 GMT+0800 (中国标准时间)
获取年份:
var date=new Date();
document.write(date.getFullYear());
输出:2015
改变时间:
var date=new Date();
date.setFullYear(2013,1,1);
document.write(date);
输出: Fri Feb 01 2013 17:00:29 GMT+0800 (中国标准时间)
时钟实例::
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body οnlοad="startTime()">
<script>
function startTime()
{
var today=new Date();
var h=today.getDate();
var m=today.getHours();
var s=today.getSeconds();
h=checkTime(h);
m=checkTime(m);
s=checkTime(s);
document.getElementById("timetest").innerHTML=h+";"+m+";"+s;
t=setTimeout(function(){
startTime();
},500);
; }
function checkTime(i)
{
if(i<10) i+='0';return i;
}
</script>
<div id="timetest"></div>
</body>
</html>
数组
concat连接:
var a=["hello ","world"];
var b=["iwen","ime"];
var c= a.concat(b);
document.write(c);
排序:
var a=["a ","b","c","t","b","e"];
a.sort();
document.write(a);
降序: var a=["1","3","2","6","4","9"];
document.write(a.sort(function(a,b){
return b-a;
}));
貌似不能将字母降序排列== 结尾追加:
var a=["1","3","2","6","4","9"];a.push("c");
document.write(a);
翻转:
var a=["1","3","2","6","4","9"];a.push("c");
document.write(a.reverse());
Math
四舍五入:
document.write(Math.round(4.5));
随机数:返回0至1 document.write(Math.random());
若是需要整数: document.write(parseInt(Math.random()*10));
最值:
document.write(Math.max(20,10,14,33));
绝对值:
document.write(Math.abs(-110));