Javescript built-in objects (JS knowledge points sum eight)

Source: Internet
Author: User

1) JS itself provides the way

用于对数据进行简便的操作,根据方法可以操作的数据类型不同,形成了不同的对象--内置对象

2) array

? A) Basic operation Method--Modify the array

从数组最后进行操作    1)数组.push(); 传入参数  添加到数组最后  返回值=>新数组    2)数组.pop();  传入参数  删除最后一个   返回值=>删除元素   从数组开始进行操作    3)数组.unshift();   传入参数 添加到数组开始  返回值=>新数组    4)数组.shift();     传入参数 删除最开始的一个     返回值=>删除元素

? b) Operating method

    1)数组.concat();  用于数组连接        特性:不会修改原数组,返回操作的结果=>新数组        特殊功能:进行数组的复制操作    2)数组.slice(start,end)   复制数组中指定部分数据        返回值:返回值:获取的结果不含end位置的值,以数组形式返回        特殊功能:            a)不传参数2,默认到最后            b)参数可以为负数.表示从后往前    3)数组.splice(start,len);  截取数组中的指定部分        返回值:截取元素的部分  数组形式        特殊用法:数组.splice(start,len,item1,item2,item3..)  将后面的元素放入原数组中被截取的位置上,个数不需要对应        常用用法:删除数组中的指定元素    

? C) Location method-IE9 not supported below

-->   数组.indexof(要查找的元素值,检索的起始位置索引值(默认是==0))   ---  查找数组中置顶元素的索引值    返回值:找到=>返回索引值   没有找到=>-1    数组去重          var arr = [1, 1, 2, 2, 3, 3, 4, 3, 2, 1, 1, 2, 3, 2, 1];        var resultArr = [];        for(var i= 0 ; i<arr.length; i++){            if(resultArr.indexof(arr[i]===-1){                resultArr.push(arr[i]);            }        }        console.log(resultArr); -->   数组.lastIndexOf()  不经常使用

? Problem: If you have more than one element in an array that satisfies the criteria, you need to use indexof multiple times

        var arr = ["a", "b", "a", "c", "a", "a", "b", "a", "c", "a", "a", "b", "a", "c", "a", "a", "b", "a", "c", "a", "a"];         var indexArr = [];//用于保存找到的索引值         var index = -1;         while (arr.indexof("a",index +1) != -1){             index = arr.indexof("a",index +1 );             indexArr.push(index);         }         console.long(indexArr);

? d) Sorting method

    1)数组.reverse( ) 翻转数组   修改原数组  返回值=>原数组    2)数组.sort( )  排序   修改原数组      3)return a-b;  升序   return b-a; 降序          var arr = [7, 5, 8, 6, 3, 11, 22, 9, 2, 1, 4];            //升序的参数形式:  function(a,b){return a-b;}            //arr.sort(function (a,b) {            //  return a-b;            //});            //降序的参数形式:   function(a,b){return b-a;}            arr.sort(function (a, b) {            return b - a;            });            console.log(arr);    

Analog sorted

        Impersonation: function sorted (arr, fn) {//fn = function (A, a, b) {//////required for use is a poor calculation                                Result//Return a-b;//Returns the difference between the current item minus latter//return b-a;//the difference between the latter minus the current item//}                var j, temp;                    for (var i = 0, i < arr.length-1; i++) {for (j = 0; J < arr.length-1-I; k + +) { Compare the size of the current and latter://Ascending criteria: Arr[j] > arr[j + 1]//ARR[J]-arr[j+1] & Gt                                        0;                                        Descending judgment Condition: Arr[j] < ARR[J + 1]//arr[j+1]-arr[j] > 0;                    Conclusion: The control of ascending and descending order is actually determined by the operand preceding the comparison operation.                    If the current item-latter difference, ascending.                    If it is latter-the difference of the current item, descending.                        if (FN (Arr[j], arr[j + 1]) > 0) {temp = Arr[j];                        ARR[J] = arr[j + 1];       Arr[j + 1] = temp;             }}}} var arr = [1, 6, 3, 5, 4, 2];                Sorted (arr, function (A, B) {//required for use is poor calculation result//Return a-b;//Returns the difference between the current item minus latter                Return b-a;//Returns the difference of latter minus the current item}); Console.log (arr);

3) string

? A) concept

字符串就是基本数据类型,不是对象;JS帮我们创建了基本包装类型对象,并且调用了其属性和方法;我们一定不要自己书写基本包装类型对象

? b) Convert to String

    1)数组.toString( );    2)join( );        //var arr = [1,2,3,4,5];        //2.1 如果不传参数,形式和toString相同        //console.log(arr.join());//"1,2,3"        //2.2 传入参数时,会按照传入的字符串将数组的所有元素进行连接        //console.log(arr.join("-"));//"1-2-3"        //2.3 传入空字符串,可以返回元素相连的字符串        //console.log(arr.join(""));//"123"

? C) Basic Properties

    1)字符串.length   字符串长度    2)索引        a)字符串[索引值]    IE9 以下不支持        b)字符串.charAt(索引值)

? d) String Method--string with immutability

    1)字符串.concat( )   字符串连接(不常用,一般使用+)    2)字符串.slice(start,end)   拷贝字符串中的指定部分字符    3)字符串.substring(start,end)   截取字符串中的指定字符(不常用)           不支持负数参数(负数默认是0 ,参数1 > 参数2 =>交换两个参数的值)    4)字符串.substr (start,len)  截取字符串中的指定长度的部分字符

? e) Uppercase and lowercase conversions

    1)字符串.toUpperCase(转为大写)    2)字符串.toLowerCase(转为小写)  

?

? f) Location method-No compatibility

    1)字符串.indexOf("要查找的字符串",起始位置索引值);        如果参数1是多个字符,会返回首字母所在的位置(前提是整体必须存在)    2)字符串.LastindexOf("要查找的字符串",起始位置索引值);

?

g) Replacement method

字符串.replace("要替换的字符串","替换为什么字符串");        replace在使用时一次只能替换从左往右的第一个指定字符。        替换次数不定时,使用while循环            var str = "adbcddefdg";            while(str.indexOf("d") != -1){            str = str.replace("d", "z");            }            console.log(str);

? L) Conversion method

字符串.split()  将字符串转换为数组结构        按照传入的字符串参数将字符串str进行分割操作            var str = "da-bac-ade-af-ag-dwd";      a) 什么也不传,整体会变成一个数组,字符串为数组的第一个元素(不常用)        console.log(str.split());//["abcdefg"]      b) 传入空字符串,转换为数组形式,每个字符为数组中的一个元素        console.log(str.split(""));//["a", "b", "c", "d", "e", "f", "g"]      c) 传入某个字符串        console.log(str.split("-"));

? e) Common use methods--combined with array method join

       1) 删除字符串中的所有a            var str = "dahuwaadawadaukbawaadua";            var arr = str.split("a");            console.log(arr.join(""));*/        2) 将字符串中的所有a替换为z            var str = "dahuwaadawadaukbawaadua";            var arr = str.split("a");//将a去除            console.log(arr.join("z"));//将z添加

4) Date Object

    1 创建方式---var date = new Date();        1.1 当前时间---var date = new Date();        1.2 具体的某一个时间---var date = new Date("2015-1-1 12:12:12");            如果传入的参数为数值,月份是从0开始,如果月份大于11,可能会跨年---var date = new Date(2015,13,1,12,12,12);--console.log(date);                    2 date对象的方法---作用:用于获取日期中的某个部分        a)获取年份---console.log(date.getFullYear());        b)获取月份:月份从0开始,使用时注意要加1---console.log(date.getMonth()+1);        c)获取日---console.log(date.getDate());        d)获取星期:  星期是从0开始的,但是0表示周日---console.log(date.getDay());        e)获取小时--- console.log(date.getHours());        f)获取分钟--- console.log(date.getMinutes());        g)获取秒---console.log(date.getSeconds());        h)获取毫秒: 0-999---1秒 是 1000毫秒---console.log(date.getMilliseconds());

5) Math Object

1)Math.PI  圆周率2)Math.max(item1,item2....)计算多个数的最大值3)Math.min(item1,item2....)计算多个数的最小值4)向上取整:取到比当前数值大的最近的一个整数    console.log(Math.ceil(2.3));//35)向下取整:取到比当前数值小的最近的一个整数    console.log(Math.floor(1.3)s);//1    console.log(Math.floor(-1.3));//-2*/    四舍五入        console.log(Math.round(1.4));        console.log(Math.round(1.5));    对于负数来说:    下面的计算方式指的是某一位上的数值,不管符号的问题        如果小于等于5,去除小数位,如果大于5,去除小数位并且将整数位的数值扩大1        console.log(Math.round(-1.5));//-1        console.log(Math.round(-1.6));//-26)Math.random()  获取随机数 [0.1)        console.log(Math.random());        传入的参数值需要是弧度,可能会出现精度的问题,后期在使用时进行处理即可7)Math.PI 表示的为180度的弧度   Math.PI/180  1弧度8)正弦值 console.log(Math.sin(Math.PI / 180 * 60));9)余弦值: console.log(Math.cos(Math.PI / 180 * 60));10)正切值: console.log(Math.tan(Math.PI / 180 * 45));

?

Javescript built-in objects (JS knowledge points sum eight)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.