es6基礎0x016:新的 Math、Number、String、Array、Object 的 Api

0x001 Math

Math更新了幾個方法,但是一般情況下沒有太大的用處

  • 反雙曲線函數,返回一個數字的反雙曲餘弦值

    Math.acosh(-1);  // NaN
    Math.acosh(0);   // NaN
    Math.acosh(0.5); // NaN
    Math.acosh(1);   // 0
    Math.acosh(2);   // 1.3169578969248166
  • 算數平方根函數,返回所有參數的算術平方根

    Math.hypot(3, 4)        // 5
    Math.hypot(3, 4, 5)     // 7.0710678118654755
    Math.hypot()            // 0
    Math.hypot(NaN)         // NaN
    Math.hypot(3, 4, "foo") // NaN, +"foo" => NaN
    Math.hypot(3, 4, "5")   // 7.0710678118654755, +"5" => 5
    Math.hypot(-3)          // 3, the same as Math.abs(-3)
  • 類C的32位整數乘法運算函數

    Math.imul(2, 4);          // 8
    Math.imul(-1, 8);         // -8
    Math.imul(-2, -2);        // 4
    Math.imul(0xffffffff, 5); // -5
    Math.imul(0xfffffffe, 5); // -10

0x002 Number

  • Number.EPSILON
    該常量表示1Number可表示的大於1的最小的浮點數之間的差值,那有什麼用呢?可以用來解決浮點數的比較問題

    x = 0.2;
    y = 0.3;
    z = 0.1;
    equal = (Math.abs(x - y + z) < Number.EPSILON); // true 
  • Number.isInteger
    該函數接受一個參數,如果該參數是整數,則返回true,否則返回falseNaN+Infinity-Infinity不是整數

    Number.isInteger(0);         // true
    Number.isInteger(1);         // true
    Number.isInteger(-100000);   // true
    
    Number.isInteger(0.1);       // false
    Number.isInteger(Math.PI);   // false
    
    Number.isInteger(Infinity);  // false
    Number.isInteger(-Infinity); // false
    Number.isInteger("10");      // false
    Number.isInteger(true);      // false
    Number.isInteger(false);     // false
    Number.isInteger([1]);       // false

0x003 String

  • String.protorype.includes(searchString[, position])
    判斷字符串是否包含子串,該函數有兩個參數,返回值爲boolean

    • searchString:要搜索的子串
    • position:可選的起始索引位置,默認就是0
    `123456`.includes(1) // true
    `123456`.includes(1, 2) // false
    `123456`.includes(7) // true
  • String.protorype.repeat(count)
    將一個字符串重複多次

    • count:重複的次數
    `12`.repeat(10) // "12121212121212121212"
    `12`.repeat(-10) // Uncaught RangeError: Invalid count value

0x004 Array

  • Array.from(arrayLike[, mapFn[, thisArg]])
    該函數可以從一個僞數組對象或者可迭代對象中創建一個數組。

    • arrayLike:目標對象
    • mapFnarrayLike到數組的映射方式
    • thisArg:映射函數中的this
    Array.from('123') //[1,2,3]
    Array.from([1,2,3]) //[1,2,3]
    Array.from(new Set([1,2,3])) //[1,2,3]
    Array.from(new Map([[1,2],[3,4],[5,6]])) // [[1,2],[3,4],[5,6]]
    Array.from('123',n=>n*2})// [2,4,6]
    
    function func(){
        return Array.from(arguments)
    }
    func(1,2,3,4) // [1,2,3,4]
  • Array.of(element0[, element1[, ...[, elementN]]])
    根據給的元素返回包含這些元素的數組

    Array.of(1) // [1]
    Array.of(1,2,3,4,5) // [1,2,3,4,5]
  • Array.fill(value[, start[, end]])
    用指定元素填充數組

    • value:要填充的元素
    • start:開始填充的位置
    • end:借宿填充的位置
    [1,2,3].fill(1,1)//[1,1,1]
    [1,2,3].fill(1,2,1)//[1,1,1]
  • Array.findIndex(callback[, thisArg])
    查找指定元素的索引

    • callback:指定命中的方式
    • thisArgcallback中的this
    [1,2,3].find(n=>n===2) // 1
    [1,2,3].find(n=>n===8) // -1
  • Array.entries()
    獲取數組迭代器

    let entries=[1,2,3].entries()
    for(let e of entries){
        console.log(e)
    }
    // (2)[0,1]
    // (2)[1,2]
    // (2)[2,3]
  • Array.keys()
    獲取數組的鍵迭代器

    let keys=[1,2,3].keys()
    for(let k of keys){
        console.log(k)
    }
    // 0
    // 1
    // 2
  • Array.values
    獲取數組的值迭代器

    let values=[1,2,3].values()
    for(let v of values){
        console.log(v)
    }
    // 1
    // 2
    // 3

0x005 Object

  • Object.assign(target, ...sources)
    對象合併,將第二個開始的參數合併到第一個,並返回一個新的對象,不存在的屬性將會添加,存在的屬性將會覆蓋。

    • target:目標對象
    • sources:源對象
    Object.assign({}, {a:1},{a:2,b:2})// {a:2,b:2}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章