ES6常用特性(二)

一、字符串的扩展

  1. 字符串的遍历器接口
    1. ES6为字符串添加了遍历器接口,可以使用for...of 循环遍历
    for(let code of 'string'){
     console.info(code);
     }
    
    
    1. includes(), startsWith(), endsWith()
      三个函数的作用分别是,判断字符串中是否包含特定字符,是否以指定字符开始,是否以指定字符结束。

    2. repeat(n) 将字符串复制n次

    3. 模板字符串

      用反引号(`)标识,嵌入的变量需要写在${}中

      function authorize(user, action) {
        if (!user.hasPrivilege(action)) {
          throw new Error(
            // 传统写法为
            // 'User '
            // + user.name
            // + ' is not authorized to do '
            // + action
            // + '.'
            `User ${user.name} is not authorized to do ${action}.`);
        }
      }
      

二、数值的扩展

  1. Number.isFinite(), Number.isNaN()
    1.用来判断是否是有限数和判断是否值为NaN

  2. Number.parseInt(), Number.parseFloat(),Number.isInteger()

    Number.isInteger("15") // false
    
  3. Number.EPSILON

    一个极小的常量,用来设置一个可以接受的误差范围。因为在JavaScript中浮点型数值的计算是不精确的,所以当误差小于Number.EPSILON可以认为结果正确。

  4. Math对象的扩展

    1. Math.trunc() 用于返回数值的整数部分
    2. 对指数函数,对数函数,三角函数的扩展 http://es6.ruanyifeng.com/#docs/number

三、 函数的扩展

  1. 函数参数的默认值
function defalutVal(x,y=120){
    console.info(x,y);
}
defalutVal(1); // 1 120
  1. 与解构赋值默认值结合使用
function defalutVal({x,y=120}){
    console.info(x,y);
}
defalutVal({});  //undefined 120  必须传入一个空对象否则报错 TypeError: Cannot match against 'undefined' or 'null'.

  1. 函数的length属性 返回没有指定默认值的参数个数
  2. rest参数 同java rest参数
function add(...addend){
    let sum = 0;
    for(let num of addend){
        sum += addend
    }
    return sum;
}

rest参数实质上将参数做为数组传入。

  1. 扩展运算符(...)

    扩展运算符将数组转为按逗号分隔的参数序列,作用域rest参数相反。

    1. 扩展运算符的用法
      1. 合并数组
        [1,2,3,...more]
        
        等价于es5中
        [1,2,3].concat(more)
        
      2. 与解构赋值结合
        const [first, ...rest] = [1, 2, 3, 4, 5];
        first // 1
        rest  // [2, 3, 4, 5]
        
        const [first, ...rest] = [];
        first // undefined
        rest  // []:
        
        const [first, ...rest] = ["foo"];
        first  // "foo"
        rest   // []
        
        如果将扩展运算符用于数组赋值,只能放在最后一个参数位置
  2. 箭头函数

    一种函数的方便写法,有多方便呢,见示例

    var f = v => v;
    

    等同于ES5中

    var f = function(v) {
      return v;
    };
    

    箭头函数有几个使用注意点。

    (1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。

    (2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。

    (3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用Rest参数代替。

    (4)不可以使用yield命令,因此箭头函数不能用作Generator函数。

四、对象的扩展

  1. 对象属性的简单表示法
{x,y}

等同于

{x:x,y:y}
  1. 对象的函数属性的简单表示法

    var fun = {
        func :function(){
            ...
        }
    }
    

    可简写为

        var fun = {
            func(){
                ...
            }
        }
    
  2. 属性表达式

    对象的属性名也可以通过表达式来定义

   let obj = {
     [propKey]: true,
     ['a' + 'bc']: 123
   };
   
   
   //另一个例子
   var lastWord = 'last word';
   var a = {
     'first word': 'hello',
     [lastWord]: 'world'
   }
   a['first word'] // "hello"
   a[lastWord] // "world"
   a['last word'] // "world"

4.Object.is() 判断两个值是否相等

   Object.is('foo', 'foo')
   // true
   Object.is({}, {})
   // false.
   Object.is(+0, -0) // false
   Object.is(NaN, NaN) // true

你可能感兴趣的:(ES6常用特性(二))