**String对象**
大小写转换
.toLowerCase()
.toUpperCase()
字符串截取
.substr() 从指定位置开始截取指定长度的字符串
.subString() 截取从指定位置到下一个指定位置之前[1,3)
查找字符位置
.indexOf() 返回指定字符第一次出现的位置
.lastIndexOf 返回指定字符最后一次出现的位置
补充字符串
.padStart(补充后字符串长度,'补充的内容') 从字符串的首位置循环填充补充的内容直至指定的长度
.padEnd(补充后字符串长度,'补充的内容') 从字符串的末位置循环填充补充的内容直至指定的长度
**Date对象**
先new Date()创建时间对象,获取的是客户端时间,不是服务器时间
new Data(new Date()); 创建当前时间对象
new Date("年-月-日")
new Date(“年/月/日”)
new Date (年,月,日,时,分,秒); 此种写法,至少指定年月,且月的索引从0开始
两个日期对象相减,返回的是毫秒数
两个日期对象相加,返回的是两个拼接的字符串
Date.parse(时间字符串); 将具有上述格式的字符串解析成毫秒数
new Date().toTimeString(); 将时间戳转换成字符串格式
new Data().toLocaleTimeString(); 将时间转换成中文加数字的形式,如:上午11:04:04
获取年
.getYear() 返回1900年到当前年份的差
.getFullYear() 返回当前年份
获取月
.getMonth()+1 返回当前月份的下标,从0开始,所以要加1
获取日
.getDate()
获取星期数
.getDay() 星期日返回0
获取小时
.getHours();
获取分钟
.getMinutes();
获取秒
.getSeconds();
获取毫秒
.getTime(); //返回日期的总毫秒数
获取年月日时分秒
**Math对象**
Math.方法名
获取随机数
Math.random() 返回0到1之间的随机数,不包括1
生成随机数方法
生成单个随机数然后字符串拼接可实现任意位数的随机数
或者四位 Math.floor(Math.random*9000+1000);
四舍五入
Math.round()
向下取整
Math.floor()
向上取整
Math.ceil()
取最大最小值
Math.min(数1,数2,...)
Math.max(数1,数2,...)
**Global对象**
将字符串转换成js代码并执行
eval("var a=123;");alert(a); 打印123
判断是否是数字
isNaN(要判断的变量),是数字则返回false,不是则返回true
eval("var a=123;");
if(!isNaN(Number(a)))
{
alert("数字") //会打印数字
}else{
alert("不是数字")
}
将字符串转换成整数
parseInt(转换的字符串)
parseInt("12abc"); 只会转换12
parseInt("abc12"); 不能转换,只会转换开头的字符串数字
将字符串转换成浮点数
parseFloat(转换的字符串)
判断对象是否是类型的示例
对象名 instanceof 类 //返回布尔值
arr instanceof Array; //判断arr是否是一个数组的示例
代码示例:
<html
>
<head
>
<title
>js 常用对象的方法
</title
>
<meta charset
="utf-8"/>
<script type
="text/javascript">
function testString()
{
var str
="adaAdsc";
var s
=str
.toLowerCase();
alert(str
.substr(0,1).toUpperCase()+str
.substring(1,str
.length
));
}
function testDate()
{
var d
=new Date();
alert(d
.getYear());
alert(d
.getFullYear());
alert(d
.getMonth()+1);
alert(d
.getDate());
alert(d
.getDay());
alert(d
.getHours());
alert(d
.getMinutes());
alert(d
.getSeconds());
}
function testMath()
{
alert(Math
.random());
alert(Math
.floor(Math
.random()*9000+1000));
}
function testGlobal()
{
eval("var a=123;");
alert(a
);
if(!isNaN(Number(a
)))
{
alert("数字")
}else{
alert("不是数字")
}
}
</script
>
</head
>
<body
>
<input type
="button" name
="" id
="" value
="测试String" onclick
="testString()" />
<input type
="button" value
="测试Date" onclick
="testDate()"/>
<input type
="button" value
="测试Math" onclick
="testMath()" />
<input type
="button" value
="测试Global" onclick
="testGlobal()" />
</body
>
</html
>