JavaScript(2)常用内置函数
数组常用方法
栈方法 push pop队列方法 unshift shiftsplice(开始index, 删除length, 插入新增)join 转换成字符串(连接符连接成一字符串) --split("""")slice(start,end)concat(array) 连接数组reverse() 数组逆序sort() 排序 降序 -- arr.sort(function(a, b){return b-a})lastIndexOf()、indexOf() -- 查找的项, 查找起点位置的索引(可选的)
filter()、map()、some()、every()、forEach() -- 数组项, 对应数组索引, 数组本身filter() -- “过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组。map() -- 指“映射”,对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。some() -- 判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。every() -- 判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true。forEach() --遍历循环、对数组中的每一项运行给定函数, 这个方法没有返回值.arr.forEach(function(item,index,array){})
字符串常用方法
.slice(start[,end]) --如果为负,将它作为length+[start, end]处理 end>=start 返回空字符串.substring(start,end) -- 如果start或end为NaN或者为负数,那么将其替换为0.substr(start[,length]) .indexOf(substr[,startIndex]) 第一次出现子字符串位置。如果没有找到子字符串,则返回-1。.lastIndexOf(substr[,startIndex]) 后面查询.split([separator[,limit]]) 将一个字符串分割为子字符串,然后将结果作为字符串数组返回 limit该值用来限制返回数组中的元素个数.toLowerCase 返回一个字符串,该字符串中的字母被转换成小写.toUpperCase 返回大写字符串.search(reExp)返回与正则表达式查找内容匹配的第一个字符串的位置.concat(str[,str2,str3]) 字符串连接.replace 用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配
日期方法
var myDate = new Date();//创建时间;myDate.getYear();(2位)//获取当前年份;myDate.getFullYear();(4位)//获取完整年份;myDate.getMonth();(0-11月,0是1月)//获取月份;myDate.getDate();(1-31日)//获取当前日期;myDate.getTime();//(从1970.1.1开始的毫秒数) //获取当前时间戳Date.now();myDate.getDay();//(0-6,0是星期天) 获取当前星期myDate.toLocaleDateString(); //获取当前日期 年月日myDate.toLocaleTimeString();//获取当前时间 时分秒myDate.getHours();(0-23) //获取当前小时数myDate.getMinutes();(0-59) //获取当前分钟数myDate.getSeconds();(0-59) //获取当前秒Date.parse();分析一个包含日期的字符串,并返回从1970.1.1开始的毫秒数new Date(year,month,0).getDate();传入年月零,返回当前月份有多少天
数学方法
Math.abs() 取绝对值
.ceil() 向上取整
.floor() 向下取整
.round() 四舍五入
.random() 随机数
function getRan(n,m,nums){ return Math.floor((Math.random()m+n)nums); }1-10 -- Math.floor(Math.random()*(10)+1)6位随机数[1-9]-- Math.floor((Math.random()9+1)100000)
转载自:https://segmentfault.com/a/1190000042532529