The built-in objects javaScript 1.Array 2.string 3. math 4.date

  Built-in objects have 1.Array 2.string 3. math 4.date   

 

1. array    most widely used mainly for storing a set of data.

     1. Creating array constructor syntax new Array (); you can also directly Array () does not recommend direct Array ( )

              2. array literal representation, is represented by [], as follows 

       // definition array 
        var Colors = new new the Array (15 );
         var cols = [15,16,17,18];

    Support multiple types 

      var infos=[6,'marry',true];

    Remove info index value of 1, the index starts from 0 NOTE 

      infos [1]; in fact is taken to marry

 

   Properties: 1. be array.length  returned as the length of the array is returned infos 3 because there are three elements

          Using array.length intercept length of   , for example, using the above infos info.length = 2 then the third element will be removed;

           Similarly array [99] = 'z', will lead array.length = 99 + 1 data even if there is no one hundred, he was the last index plus 1;   

var cols=[15,16,17,18];
        cols[20]=120;
        for (var i=0;i<=cols.length;i++){
            console.log(i+','+cols[i]);
        }

result:

 

 

 

   Array.push (data)  is inserted into the end of the array, return length  

   Array.unshift (data) is inserted to the head array and returns length  

   array.pop () to delete the last return value deleted

   array.shift () to delete the last first, delete the return value

   array.reserver () the contents of the array in turn, but not ordering, is exactly the opposite of the original order

   Array.join () to connect the array together into a string, for example, array (15,16,17) using array.join ( "-") 15-16-17 result is not specified as a default connection 15, 16 is a comma 17

  array.sort(sortby )数组排序,不用说,经常用到  默认是 从小到大ASCII码     括号里可以使用函数  

例 

 var nums=new Array(45,23,12,66,9);
            nums.sort(function (a,b) {
                return a-b;
            })
        console.log(nums);
// 结果 从小到大
  [9, 12, 23, 45, 66]
 var nums=new Array(45,23,12,66,9);
            nums.sort(function (a,b) {
                return b-a;
            })
        console.log(nums);
            // 结果从大到小  [66, 45, 23, 12, 9]

 

    array.concat(arr2,arr3,...)  意思是array 连接arr2合并成一个  :    用于连接两个或者多个数组  

    array.slice(start,end) :  从已有的数组中返回选定的元素  start  如果是负数,则从尾部开始算起    end  规定从何处结束 不包含该元素。

   array.splice () 方法强大 能删数组内容 , 能添加数组内容,能修改数组内容

   array.splice (index,count)   删除从index处开始的零个或者多个元素    返回含有本删除的元素的数组, count是要删除的项目数量, 如果为0 则不会删除,如果不设置则删除从index开始所有值。

    array.splice (index,0,item1,item2,item3.....)  插入从index处开始的多个元素    插入到第index 个索引前面 

    array.splice (index,count,item1,item2,item3.....)   先删除在插入 

  array.indexOf(searchvalue,startIndex)    从数组的开头位置向后查找   searchvalues   需要查找的内容,  必须要填写,  返回number 查找的项在数组中的位置,没有找到的情况下返回-1.

  array.lastIndexOf(searchvalue,startIndex)    从数组的末尾位置向后查找   searchvalues   需要查找的内容,  必须要填写,  返回number 查找的项在数组中的位置,没有找到的情况下返回-1.

 

 

 

2. string

 

查找

   string.charCodeAt(index)   返回string 中 index 位置字符的字符编码  。

    string.charAt(index)           返回string 中 index 位置的字符  。es5 中可以直接用   例  var str='dsadasd'   取出来可以用 str[1]   得到 s   结果 

  string.indexOf(''searchvalues'')     从一个字符串中搜索给定的子字符串,返回子字符串的位置 找不到为-1

 string.LastindexOf(''searchvalues'')     从右侧检测字符串中给定的子字符串位置,返回最后一个出现子字符串的位置      找不到为-1

 

截取   

    

   string.slice(start,end) :  从已有的字符串中截取选定的元素  start  如果是负数,则从尾部开始算起    end  规定从何处结束 不包含该元素。

   string.substring(start,end) : 同上   从已有的字符串中截取选定的元素  区别  start  如果是负数 ,自动将参数转换为0

   string.substr(start,len) : 同上   从已有的字符串中截取选定的元素  区别  start  如果是负数 ,会将传入的负值与字符串的长度相加。 当len 为负数时候 返回空字符串

 

 // 声明用户名的变量    结果是  txt
        var week='index.txt';
        var gettxt=week.slice(week.lastIndexOf('.'));
        console.log(gettxt.substr(1));

   string.split (separator) 把一个字符串分割成数组  separator 分隔符

   string.replace (a,b) 将a的值 替换成b   

   string.toUpperCase()将字符串转换成大写

   string.toLowerCase()将字符串转换成小写

案例:驼峰转换

 

 

  // 驼峰转换 方法1  
        var week='border-left-button'; //berderLeftBotton
        function turnUp(str,spl) {
            for (var i=0;i<str.length;i++){
                var check=str.indexOf(spl);
                if (check!=-1){
                    // 等于-1是 未查询到 
                    // 截取字符串变大写
                    var tran=str.substr(check+1,1).toUpperCase();
                    //截取那两个字符串 -和-后面的一个字母
                    var tex=str.substr(check,2);
                    // 替换成大写 
                    str=str.replace(tex,tran);
                }
            }
            return str;
        }
        var res=turnUp(week,'-');
        console.log(res);
       // 结果  borderLeftButton

 

 //方法二转换成数组 
        var week='border-left-buttob'; //berderLeftBotton
        var arrweek=week.split('-');
        for (var i=0; i<arrweek.length;i++){
            if (i==0){
                //s首字母不需要
                continue;
            }else {
                //不是首字母转换
               var  weekUp=arrweek[i].substr(0,1);
               arrweek[i]=arrweek[i].replace(weekUp,weekUp.toUpperCase());
            }
        }
         console.log(arrweek.join(''));
        // 结果同上

3. math 对象

 

min()   一组数据的最小值

max()  一组数据的最大值

ceil()     向上取整

floor()    向下取整

round()  四舍五入

abs()    返回绝对值  求绝对值 

random()  默认随机一个 0-1的数据         求 n到m 之间 有  math.random()*((m-n+1)+n )向下取整

 

 

4. date 对象

   语法  new  Date();

   功能: 创建一个日期时间对象

   返回值: 在不传参的情况下,返回当前的日期时间对象

    getFullYear(): 返回四位数的年份

    getMonth(): 返回日期中的月份,返回值未0-11   0标识1一月  11标识 12月

    getDate(): 返回月份中的天数

    getday(): 返回星期 返回值未0-6

    getHours(): 返回小时 

    getMinutes():返回分

    getSeconds(): 返回秒

    getTime(): 返回标识日期的毫秒数  

   设置年月日时分秒以及星期的方法

   setFullYear(): 设置四位数的年份

    setMonth(): 设置日期中的月份,返回值未0-11   0标识1一月  11标识 12月

   setDate(): 设置每月的天数

 setDay(): 设置星期 返回值未0-6

   settHours(): 设置小时 

   setMinutes():设置

   setSeconds(): 设置秒

   setTime(): 设置标识日期的毫秒数  

 

Guess you like

Origin www.cnblogs.com/tongcharge/p/11598829.html