javascript中常用方法

来源:互联网 发布:海康ddns域名取消了 编辑:程序博客网 时间:2024/06/08 05:04
  1. Array
'use strict';let cout = function(){    console.log('arr1 :'+ arr1);    console.log('arr2 :'+ arr2);    for(let i=0;i<arguments.length;i+=1){        console.log(arguments[i]);    }}let arr1 = [1,2,13,15,23];let arr2 = ['a','b','c'];let arrCat = arr1.concat(arr2,'d');//c = [ 1, 2, 13, 15, 23, 'a', 'b', 'c', 'd' ]console.log(arrCat.join());//返回一个数组中元素组成的字符串,以‘,’隔开//1,2,13,15,23,a,b,c,dconsole.log(arrCat.join(''));//没有分隔符//12131523abcdarrCat.pop();//删除最后一个元素//shift  删除第一个元素arrCat.push(['d','e','f']);//在末尾添加一个元素//unshift 在头部添加元素//[ 1, 2, 13, 15, 23, 'a', 'b', 'c', [ 'd', 'e', 'f' ] ]var b=arrCat;//引用复制b.reverse();//数组反转console.log(b);//[ [ 'd', 'e', 'f' ], 'c', 'b', 'a', 23, 15, 13, 2, 1 ]console.log(arrCat);//[ [ 'd', 'e', 'f' ], 'c', 'b', 'a', 23, 15, 13, 2, 1 ]b.sort();//默认转成字符串后排序//[ 1, 13, 15, 2, 23, 'a', 'b', 'c', [ 'd', 'e', 'f' ] ]b.sort(function(a,b){    if(a===b){        return 0;    }    if(typeof a === typeof b){        return a<b?-1:1;    }else{        return typeof a < typeof b ? -1 : 1;    }})//[ 1, 2, 13, 15, 23, [ 'd', 'e', 'f' ], 'a', 'b', 'c' ]b.splice(0,2,9,[23,24]);//第一参数为要删除的下标,第二参数为删除数,后面参数为要在此位置上添加的元素//b === [ 9, [ 23, 24 ], 13, 15, 23, [ 'd', 'e', 'f' ], 'a', 'b', 'c' ]let c = b.slice(1,2);//第一参数为开始截取位置,第二参数为结束截取位置//c = [ [ 23, 24 ] ]
  1. Function
    Function.apply(thisArg,argArray)
  2. Number

  3. Object
    Object.hasOwnProperty(name)

  4. RegExp
    regexp.exec(string)//返回数组
    regexp.test(string)//返回boolean