JS内置对象Date、string、math使用及案例

1.内置对象(语言自带的对象,提供了常用的基本的功能)

打印数组和字符串不用for…in….(JSon才用)
Arguments 函数参数集合
Array 数组
Boolean 布尔对象
Date 日期时间
Error 异常对象
Function 函数构造器
Math 数学对象
Number 数值对象
Object 基础对象
RegExp 正则表达式对象
String 字符型对象

2.Date对象的方法

JS内置对象Date、string、math使用及案例

getTime():获取累计毫秒数,从1970/1/1开始
var dat1=Date.now();
 var dat2=+new Date();
 var dat3=new Date().getTime();
 var dat4=new Date().valueOf()

3. String对象

a) 给索引查字符(char At/charCodeAt)
该String对象的charAt()方法返回一个新字符串,该字符串由位于字符串中指定偏移量的单个UTF-16代码单元组成。

character = str .charAt(index)

b) 给字符查索引(indexOf/LastIndexOf)
如果两个查相同的字符并且字符串里面有两个相同的字符返回的索引值不一样

c) URI编码和解码
全称通用资源标识符,数据传输的时候经过编码再传递
encodeURIComponent/decodeURIComponent 编码/解码

d) 字符串的连接(返回一个新字符串,并且不会被修改)

var str3=str1.concat(str2);

e) 字符串的截取

slice()  
console.log(str.slice(2));//从索引截取到最后
console.log(str.slice(2,5));//从索引值开始包左不包右
console.log(str.slice(-3));//后几个
console.log(str.slice(5,2));//空字符串
substr()
console.log(str.substr(2));//从索引截取到最后
console.log(str.substr(2,6));//从索引值开始截取多少长度字符串
console.log(str.substr(-3));//后几个
console.log(str.substr(5,2));//e
substring()同slice()
console.log(str.substring(2));//从索引截取到最后
console.log(str.substring(2,6));//从索引值开始截取多少长度字符串
console.log(str.substring(-3));//全部截取
console.log(str.substring(5,2));//只能调换

f) trim()去除前后空格
replace()替换

console.log(str2.replace(/today/g,"tomorrow"));全部替换使用正则

g) 字符串变数组
Split()无参是把字符串作为一个元素添加进数组,有参数是把每个元素分开

h) 大小写转换
str.toLowerCase:转换成小写
str.toUpperCase:转换成大写

i) 创建标签
var str1=“你好”;
console.log(str1.anchor());
console.log(str1.big());
console.log(str1.sub());
console.log(str1.sup());
console.log(str1.link(“http:www.baidu.com”));
console.log(str.bold());

4. 字符串案例

求一个字符串占有几个字符位(事件/06)

5. Math

a) Math.abs() 取绝对值
b) Math.floor() 向下取整
c) Math.ceil() 向上取整
d) Math.round() 四舍五入取整
e) Math.random() 随机数