美文网首页程序员
关于javascript内置对象总结

关于javascript内置对象总结

作者: 蓝海00 | 来源:发表于2018-07-01 00:04 被阅读0次

    2018-07-01

    (常用总结,部分方法有本人自己的代码示范,如有错误请联系我q:1444133004,来自一个前端小白的倔强)

    1.Math(常用)

    ​ Math是一个对象,但不是一个函数,Math对象下的属性和方法都是静态的。

    1.1 Math.ceil():向上取整

    Math.ceil(10.1);//11


    1.2 Math.floor();向下取整

    Math.floor(10.5);//10


    1.3 Math.PI;圆周率的值

    Math.PI;//3.141592653589793


    1.4 Math.max();一组数字中的最大值

    Math.max(10,20,30);//30


    1.5 Math.min();一组数字中的最小值

    ​ Math.min(10,20,30);//10


    1.6 Math.abs();绝对值

    Math.abs(-10);//10


    1.7 Math.random();随机数

    ​ Math.random()*100+1//1-100的随机数


    1.8 Math.round();四舍五入取整

    Math.round(20.49);//20

    ​ Math.round(-20.51);//-21


    2.Date(常用)

    ​ 获取系统时间(小于10在前面+“0”)。

    ​ var dt = new Date();

    2.1 getFullYear();获取系统当前年份


    2.2 getMonth()+1;获取系统当前月份(从0开始,所以要+1)


    2.3 getDate();获取系统当前日


    2.4 getHours();获取系统当前小时


    2.5 getMinutes();获取系统当前分钟


    2.6 getSeconds();获取系统当前秒


    2.7 getDay();获取系统当前星期—从0开始,0为星期天


    2.8 toLocaleDateString();获取系统当前China日期如:2018/6/28


    2.9 toLocaleTimeString();获取系统当前China时间如:下午6:52:20


    2.10 valueOf();获取系统当前毫秒,时间戳


    3.String(常用)

    ​ String全局变量是一个用于字符串或一个字符序列的构造函数。

    3.1 .length;字符串长度


    3.2 .charAt(索引);返回值是指定索引位置的字符串,超出索引,结果是空字符串


    3.3 .concat(字符串1,字符串2,...);返回的是拼接之后的新的字符串

    var st1 = "21";

    var st2 = "34";

    console.log(st1.concat(st2));//2134


    3.4 .indexOf(要找的字符串,从某个位置开始的索引); 返回的是这个字符串的索引值,没找到则返回-1

    var st = "莫惨哦是哪里安心那希腊神";

    console.log(st.indexOf("是",0));//3


    3.5 .lastIndexOf(要找的字符串);从后向前找,但是索引仍然是从左向右的方式,找不到则返回-1

    var st = "莫惨哦是哪里安心那希腊是神";

    console.log(st.lastIndexOf("是"));//11


    3.6 .replace("原来的字符串","新的字符串");用来替换字符串的

    var st = "你是";

    console.log(st.replace("你","w"));//w是


    3.7 .slice(开始的索引,结束的索引); 从索引5的位置开始提取,到索引为10的前一个结束,没有10,并返回这个提取后的字符串

    var st = "你是谁我是谁一二";

    console.log(st.slice(0,2));//你是


    3.8 .split("要干掉的字符串",切割后留下的个数);切割字符串

    var myString = "Hello World. How are you doing?";

    var splits = myString.split(" ", 3);

    console.log(splits);//["Hello", "World.", "How"]


    3.9 .substr(开始的位置,个数);返回的是截取后的新的字符串

    var st = "一二三四五六七八九十";

    console.log(st.substr(2,5));//三四五六七


    3.10 .toLocaleLowerCase(); .toLowerCase();转小写,建议用.toLocaleLowerCase();


    3.11 .toLocaleUpperCase(); .toUpperCase();转大写,建议用 .toLocaleUpperCase();


    3.12 .trim();干掉字符串两端的空格


    4.Array(常用)

    ​ Array对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。

    4.1 Array.isArray(对象);判断这个对象是不是数组

    var arr = [1,2,3,4];

    console.log(Array.isArray(arr));//true


    4.2 .concat(数组,数组,数组,...);组合一个新的数组,数组拼接

    var arr = [1,2,3,4];

    var arr1 = [88,88,6];

    var arr2 = ["你和","哈哈","呼呼"];

    console.log(arr.concat(arr1,arr2));//[1, 2, 3, 4, 88, 88, 6, "你和", "哈哈", "呼呼"]


    4.3 .every(函数);返回值是布尔类型,函数作为参数使用,函数中有三个参数,第一个参数是元素的值,第二个参数是索引值,第三个参数是原来的数组(没用)如果这个数组中的每个元素的值都符合条件,最后才返回的是true

    测试数组中的所有元素是否都大于10。

    function isBigEnough(element, index, array) {

      return element >= 10;

    }

    [12, 5, 8, 130, 44].every(isBigEnough);  // false

    [12, 54, 18, 130, 44].every(isBigEnough); // true

    优化兼容旧环境代码:

    if (!Array.prototype.every) {

      Array.prototype.every = function(callbackfn, thisArg) {

        'use strict';

        var T, k;

        if (this == null) {

          throw new TypeError('this is null or not defined');

        }

        // 1. Let O be the result of calling ToObject passing the this

        //    value as the argument.

        var O = Object(this);

        // 2. Let lenValue be the result of calling the Get internal method

        //    of O with the argument "length".

        // 3. Let len be ToUint32(lenValue).

        var len = O.length >>> 0;

        // 4. If IsCallable(callbackfn) is false, throw a TypeError exception.

        if (typeof callbackfn !== 'function') {

          throw new TypeError();

        }

        // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.

        if (arguments.length > 1) {

          T = thisArg;

        }

        // 6. Let k be 0.

        k = 0;

        // 7. Repeat, while k < len

        while (k < len) {

          var kValue;

          // a. Let Pk be ToString(k).

          //  This is implicit for LHS operands of the in operator

          // b. Let kPresent be the result of calling the HasProperty internal

          //    method of O with argument Pk.

          //  This step can be combined with c

          // c. If kPresent is true, then

          if (k in O) {

            // i. Let kValue be the result of calling the Get internal method

            //    of O with argument Pk.

            kValue = O[k];

            // ii. Let testResult be the result of calling the Call internal method

            //    of callbackfn with T as the this value and argument list

            //    containing kValue, k, and O.

            var testResult = callbackfn.call(T, kValue, k, O);

            // iii. If ToBoolean(testResult) is false, return false.

            if (!testResult) {

              return false;

            }

          }

          k++;

        }

        return true;

      };

    }


    4.4 .filter(函数);返回的是数组中每一个元素都复合条件的元素,组成了一个新的数组只返回符合条件的

    function isArr(ele) {

        return ele.length > 6;

    }

    console.log(['sray', 'limt', 'elte', 'exuberant'].filter(isArr));//["exuberant"]

    优化兼容旧环境代码:

    if (!Array.prototype.filter)

    {

      Array.prototype.filter = function(fun /* , thisArg*/)

      {

        "use strict";

        if (this === void 0 || this === null)

          throw new TypeError();

        var t = Object(this);

        var len = t.length >>> 0;

        if (typeof fun !== "function")

          throw new TypeError();

        var res = [];

        var thisArg = arguments.length >= 2 ? arguments[1] : void 0;

        for (var i = 0; i < len; i++)

        {

          if (i in t)

          {

            var val = t[i];

            // NOTE: Technically this should Object.defineProperty at

            //      the next index, as push can be affected by

            //      properties on Object.prototype and Array.prototype.

            //      But that method's new, and collisions should be

            //      rare, so use the more-compatible alternative.

            if (fun.call(thisArg, val, i, t))

              res.push(val);

          }

        }

        return res;

      };

    }


    4.5 .push(值);把值追加到数组中,加到最后,返回值是追加数据之后的数组长度

    var arr = [1,2,"你"];

    arr.push("哈",4);

    console.log(arr);//[1, 2, "你", "哈", 4]


    4.6 .pop();删除数组中最后一个元素,返回值就是删除的这个值

    var arr = [1,2,"你"];

    arr.pop();

    console.log(arr);//[1, 2]


    4.7 .shift();删除数组中第一个元素,返回值就是删除的这个值

    var arr = [1,2,"你"];

    arr.shift();

    console.log(arr);//[2, "你"]


    4.8 .unshift(值);向数组的第一个元素前面插入新的元素,返回值是插入后的数组的长度

    var arr = [1,2,"你"];

    arr.unshift("哈",4);

    console.log(arr);//["哈", 4, 1, 2, "你"]


    4.9 .forEach(函数);遍历数组用---相当于for循环

    var array1 = ['a', 'b', 'c'];

    array1.forEach(function(element) {

        console.log(element);

    });

    //a

    //b

    //c

    优化兼容旧环境代码:

    // Production steps of ECMA-262, Edition 5, 15.4.4.18

    // Reference: http://es5.github.io/#x15.4.4.18

    if (!Array.prototype.forEach) {

      Array.prototype.forEach = function(callback, thisArg) {

        var T, k;

        if (this == null) {

          throw new TypeError(' this is null or not defined');

        }

        // 1. Let O be the result of calling toObject() passing the

        // |this| value as the argument.

        var O = Object(this);

        // 2. Let lenValue be the result of calling the Get() internal

        // method of O with the argument "length".

        // 3. Let len be toUint32(lenValue).

        var len = O.length >>> 0;

        // 4. If isCallable(callback) is false, throw a TypeError exception.

        // See: http://es5.github.com/#x9.11

        if (typeof callback !== "function") {

          throw new TypeError(callback + ' is not a function');

        }

        // 5. If thisArg was supplied, let T be thisArg; else let

        // T be undefined.

        if (arguments.length > 1) {

          T = thisArg;

        }

        // 6. Let k be 0

        k = 0;

        // 7. Repeat, while k < len

        while (k < len) {

          var kValue;

          // a. Let Pk be ToString(k).

          //    This is implicit for LHS operands of the in operator

          // b. Let kPresent be the result of calling the HasProperty

          //    internal method of O with argument Pk.

          //    This step can be combined with c

          // c. If kPresent is true, then

          if (k in O) {

            // i. Let kValue be the result of calling the Get internal

            // method of O with argument Pk.

            kValue = O[k];

            // ii. Call the Call internal method of callback with T as

            // the this value and argument list containing kValue, k, and O.

            callback.call(T, kValue, k, O);

          }

          // d. Increase k by 1.

          k++;

        }

        // 8. return undefined

      };

    }


    4.10 .indexOf(元素值);返回的是索引,没有则是-1

    var array1 = ['a', 'b', 'c'];

    console.log(array1.indexOf("b"));//1


    4.11 .map(函数);数组中的每个元素都要执行这个函数,把执行后的结果重新的全部的放在一个新的数组中

    var arr = [1,2,3];

    console.log(arr.map(function (a) {

        return a+1;

    }));//[2, 3, 4]


    4.12 .reverse();反转数组

    var arr = [1, 2, "哈哈"];

    console.log(arr.reverse());//["哈哈", 2, 1]


    4.13 .sort();排序的,可能不稳定,如果不稳定,请写MDN中的那个固定的代码

    4.14 .slice(开始的索引,结束的索引);把截取后的数组的值放在一个新的数组中,但是不包含结束的索引对应的元素值

    var arr = [1, 2, "哈哈",5,"嘿嘿",9];

    console.log(arr.slice(1,4));//[2, "哈哈", 5]


    4.15 .splice(开始的位置,要删除的个数,替换的元素的值);一般是用于删除数组中的元素,或者是替换元素,或者是插入元素

    相关文章

      网友评论

        本文标题:关于javascript内置对象总结

        本文链接:https://www.haomeiwen.com/subject/xlwxuftx.html