ES6 对象和数组扩展

251 阅读3分钟

对象

对象字面量语法扩展

ES6改进对象字面量,使其变得更简洁、更强大。通过下面几个语法体现:

  • 属性初始值简写
  • 对象方法简写
  • 可计算属性名
//  属性初始值简写
function fun (name, age) {
    return {
        name,   //  相当于 name: name
        age     //  相当于 age: age
    };
}

//  对象方法简写
var object = {
    name: "ES6",
    getName () {
        return this.name;
    }
    /*  相当于
        getName: function () {}
    */
}

//  可计算属性名
var key = "JavaScript";
var object = {
    [key]: "ES6"
    //  key 将被转化为一个字符串 "JavaScript" ;
}
console.log(object[key]);
console.log(object.JavaScript);

重复对象字面量属性:在 ES5 严格模式下会抛出语法错误;在 ES6 中对应重复属性都选取最后一个取值。

新增方法

Object.is()

用于比较两个参数类型与值。这与全等运算符(===)在大部分情况下运行结果是相同的。唯一区别在于 +0 和 -0,NaN 和 Nan 运行结果不同。

console.log(+0 === -0);             //  true
console.log(Object.is(+0, -0));     //  false

console.log(NaN === NaN);           //  false
console.log(Object.is(NaN, NaN));   //  true
Object.assgin()
  • 用于将所有可枚举属性的值从一个或多个源对象复制到目标对象,返回目标对象。
  • 当对象中只有一级属性,没有二级属性的时候,此方法为深拷贝,但是对象中有对象的时候,此方法,在二级属性以后就是浅拷贝。
  • 由于Object.assign() 执行赋值操作,因此源对象访问器属性最终转变为目标对象中的一个数据属性。
var object = {
    name: 1,
    age: 2
};
var cloneObject = Object.assign({}, object);
console.log(cloneObject);   //  Object { name: 1, age: 2 }



对象原型扩展

Object.setPrototypeOf()

ES6 加入对象实例化后改变原型的标准方法 Object.setPrototypeOf(),通过该方法可以改变任意指定对象的原型,接受两个参数:被改变原型的对象及替代目标对象的原型。

var prototype = {
    name: "ES6"
};
var object = Object.create({ name: "ES5"});
console.log(Object.getPrototypeOf(object));     //  Object { name: "ES5" }

Object.setPrototypeOf(object, prototype);
console.log(Object.getPrototypeOf(object));     //  Object { name: "ES6" }
Super 引用

ES6 引入 Super 特性,使用它可以更便捷地访问对象原型。

//  ES5 版本
var prototype = { getName () { return "111";} };
var object = {
    getName () {
        return Object.getPrototypeOf(this).getName.call(this);
    }
}
Object.setPrototypeOf(object, prototype);
console.log(object.getName());

//  ES6 版本
let prototype = { getName () { return "111";} };
let object = {
    getName () {
        return super.getName();
    }
}
Object.setPrototypeOf(object, prototype);
console.log(object.getName());

注意:Super 引用必须在对象使用简写方法的条件下,在其他方法声明会抛出语法错误。

var object = {
    getName: function () {
        return super.getName();     //  抛出错误
    }
}



数组

新增方法

  • Array.of()
  • Array.from()
  • find() 和 findIndex()
  • fill()

Array.of() 改进 Array 构造函数。唯一区别是:

var array1 = Array(-1);
console.log(array1.length);     //  8
var array2 = Array.of(8);
console.log(array2.length);     //  1

在只传入一个整型数据参数时,Array构造函数当作该实例数组的长度,而Array.of()则当作数组中的元素值。

Array.from() 用于将可迭代对象或类数组对象转换为真正的数组。

function fun () {
    console.log(arguments instanceof Array);
    return Array.from(arguments);
}
var array = fun(1,2,3);                 //  false
console.log(array instanceof Array);    //  true



find() 用于寻找第一个符合特定条件的元素值。
findIndex() 用于寻找第一个符合特定条件元素的位置。 两个方法接受两个参数相同:

  • 第一个回调函数
  • 第二个可选参数,表示回调函数中的 this 值
var array = [1,3,6,1,4,8,10];
var value = array.find(function (value, index, array) {
    return value % 2 === 0;    
});
var index = array.findIndex(function (value, index, array) {
    return value % 2 === 0;
});
console.log(value);     //  6
console.log(index);     //  2



fill() 用一个固定值填充一个数组中从起始索引到终止索引内(不包括终止索引)的全部元素。改变当前数组。
参数:

  • 第一个参数:用来填充数组元素的值
  • 第二个可选参数,起始索引,默认值为0
  • 第三个可选参数,终止索引,默认值为 this.length
var array = [1,2,3,4];
array.fill(1);
console.log( array );   //  Array(4) [ 1, 1, 1, 1 ]
array.fill(2,1,2);
console.log( array );   //  Array(4) [ 1, 2, 1, 1 ]