本文主要记录JavaScript常用的API,其中包括Object,String,Array,Number,Math,Date和JSON七个对象的API。
Object
- Object.prototype() // 返回原型对象
- Object.hasOwnProperty() // 判断某个属性是否为当前对象自身的属性
- Object.toString() // 返回当前对象对应的字符串形式
- Object.keys() // 遍历对象的可枚举属性
- Object.getOwnPropertyName() // 遍历对象不可枚举的属性
String
- String.length() // 返回字符串长度
- String.replace() // 查找匹配字符并替换
- String.splice() // 提取匹配字符串
- String.split() // 分割字符串为数组
- String.chatAt() // 返回在指定位置的字符
- String.concat() // 连接多个字符串
- String.trim() // 去除两边空格
- String.toLowerCase() // 字符串转为小写
- String.toUpperCase() // 字符串转为大写
Array
Array.length() // 数组长度
Array.sort() // 数组元素排序
Array.reserve() // 数组元素反转
Array.concat() // 连接多个数组
Array.push() // 添加数组末尾元素
Array.pop() // 删除数组末尾元素
Array.shift() // 删除数组第一位元素
Array.unshift() // 添加数组第一位元素
Array.join() // 用数组元素组成字符串
Array.reduce() // 遍历数组
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Array.map() // 遍历数组
array.map(function(currentValue,index,arr), thisValue)
Array.forEach() // 遍历数组
array.forEach(function(currentValue, index, arr), thisValue)
Array.filter() // 过滤数组
array.filter(function(currentValue,index,arr), thisValue)
Array.indexOf() // 搜索数组
array.indexOf(item,start)
Number
- Number.toString() // 将数值转为字符串
- Number.toFixed() // 将数值转为指定位数的小数
- Number.toPrecision() // 将数值转为指定位数的有效小数
Math
- Math.random() // 返回0到1之间的一个随机数,区间为[0,1)
- Math.pow() // 指数运算
- Math.sqrt() // 返回参数的平方根
- Math.abs() // 返回参数的绝对值
- Math.round() // 四舍五入
- Math.ceil() // 向上取整
- Math.floor() // 向下取整
Date
- Date.getDate() // 回一个月中的某一天(1 ~ 31)
- Date.getDay() // 返回一周中的某一天(0 ~ 6)
- Date.getHours() // 返回小时(0 ~ 23)
- Date.getMinutes() // 返回分钟(0 ~ 59)
- Date.getSeconds() // 返回秒数(0 ~ 59)
- Date.getMonth() // 返回月份(0 ~ 11)
- Date.getFullYear() // 返回四位数字年份
- Date.getTime() // 返回1970年1月1日至今的毫秒数
JSON
- JSON.stringify() // 用于将对象转化成JSON字符串
- JSON.parse() // 用于将JSON字符串转化成对象。