ES6(5)对象

208 阅读10分钟

属性的简洁表示法(对象短命名)

如果对象的属性名和变量名一样的话可以二合一

//以前
let name='aaa',age=18;

var obj={
    name:name,
    age:age
}
console.log(obj);		//{ name: 'aaa', aßßge: 18 }

//现在
let name='aaa',age=18;

let obj={
    name,
    age
}
console.log(obj);	//{ name: 'aaa', age: 18 }

方法也可以简写

const o = {
  method() {
    return "Hello!";
  }
};

// 等同于
const o = {
  method: function() {
    return "Hello!";
  }
};

下面是一个实际的例子

let birth = '2000/01/01';

const Person = {

  name: '张三',

  //等同于birth: birth
  birth,

  // 等同于hello: function ()...
  hello() { console.log('我的名字是', this.name); }
};

属性的赋值器(setter)和取值器(getter),事实上也是采用这种写法

const cart = {
    count: 4,

    get count2 () {
        return this.count;
    },

    set count2 (value) {
        if (value < this.count) {
            throw new Error('数值太小了!');
        }
        this.count = value;
    }
}

console.log(cart);
console.log(cart.count);
console.log(cart.count2);

//{ count: 4, count2: [Getter/Setter] }
//4
//4

属性名表达式,obj['a' + 'bc']

JS定义对象的属性,有两种方法

// 方法一:直接用标识符作为属性名
obj.foo = true;

// 方法二:用表达式作为属性名
obj['a' + 'bc'] = 123;

但是,如果使用字面量方式定义对象(使用大括号),在 ES5 中只能使用方法一(标识符)定义属性。

ES6 允许字面量定义对象时,用方法二(表达式)作为对象的属性名,即把表达式放在方括号内

let propKey = 'foo';

let obj = {
  [propKey]: true,
  ['a' + 'bc']: 123
};
let lastWord = 'last word';

const a = {
  'first word': 'hello',
  [lastWord]: 'world'
};

a['first word'] // "hello"
a[lastWord] // "world"
a['last word'] // "world"

表达式可以用于定义方法名

let obj = {
  ['h' + 'ello']() {
    return 'hi';
  }
};

obj.hello() // hi

属性名表达式如果是一个对象,默认情况下会自动将对象转为字符串[object Object]

const keyA = {a: 1};
const keyB = {b: 2};

const myObject = {
  [keyA]: 'valueA',
  [keyB]: 'valueB'
};

myObject // Object {[object Object]: "valueB"}

属性名表达式与简洁表示法,不能同时使用

// 报错
const foo = 'bar';
const bar = 'abc';
const baz = { [foo] };

// 正确
const foo = 'bar';
const baz = { [foo]: 'abc'};

方法的name属性,返回函数名

函数的name属性,返回函数名,对象方法也是函数,因此也有name属性。

const person = {
  sayName() {
    console.log('hello!');
  },
};

person.sayName.name   // "sayName"

super

通过super调用原型链上的方法(构造函数的方法)

let obj1={name:'a',getName(){
        return 'nnn'
    }}
let obj2={
    __proto__:obj1,
    getName(){
        return 'mmm'
    }
}
console.log(obj2.getName());  //'mmm'


let obj1={name:'a',getName(){
        return 'nnn'
    }}
let obj2={
    __proto__:obj1,
    getName(){
        return 'mmm'+super.getName();
    }
}
console.log(obj2.getName());	//'mmmnnn'

对象的扩展方法

Object.is(A,B),比较两个值是否严格相等

用来比较两个值是否严格相等,与严格比较运算符(===)的行为基本一致。

不同之处只有两个:

  • 一是+0不等于-0
  • 二是NaN等于自身

===的缺陷:NaN不等于自身,以及+0等于-0

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

Object.is(+0, -0) // false
Object.is(NaN, NaN) // true

Object.is的实现

Object.defineProperty(Object, 'is', {
  value: function(x, y) {
    if (x === y) {
      // 针对+0 不等于 -0的情况
      return x !== 0 || 1 / x === 1 / y;
    }
    // 针对NaN的情况
    return x !== x && y !== y;
  },
  configurable: true,
  enumerable: false,
  writable: true
});

Object.assign(目标对象,源对象1,源对象2,....)

用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target)。

const target = { a: 1 };

const source1 = { b: 2 };
const source2 = { c: 3 };

Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}

有同名属性,后面的属性会覆盖前面的属性

如果目标对象与源对象有同名属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性

const target = { a: 1, b: 1 };

const source1 = { b: 2, c: 2 };
const source2 = { c: 3 };

Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}

只有一个参数(只有一个目标对象)会直接返回该参数

只有一个参数,Object.assign会直接返回该参数

const obj = {a: 1};
Object.assign(obj) === obj // true

如果该参数不是对象,则会先转成对象,然后返回。

typeof Object.assign(2) // "object"

由于undefined和null无法转成对象,所以如果它们作为参数,就会报错

Object.assign(undefined) // 报错
Object.assign(null) // 报错

非对象参数出现在源对象的位置,都会转成对象,如果无法转成对象,就会跳过

undefined和null不在首参数,就不会报错

如果非对象参数出现在源对象的位置(即非首参数),这些参数都会转成对象,如果无法转成对象,就会跳过。这意味着,如果undefined和null不在首参数,就不会报错

let obj = {a: 1};
Object.assign(obj, undefined) === obj // true
Object.assign(obj, null) === obj // true

其他类型的值(即数值、字符串和布尔值)不在首参数

字符串会以数组形式,拷贝入目标对象,其他值都不会产生效果

这是因为只有字符串的包装对象,会产生可枚举属

const v1 = 'abc';
const v2 = true;
const v3 = 10;

const obj = Object.assign({}, v1, v2, v3);
console.log(obj); // { "0": "a", "1": "b", "2": "c" }

Object.assign方法实行的是浅拷贝

如果源对象某个属性的值是对象,那么目标对象拷贝得到的是这个对象的引用。

源对象的对象属性改变,目标对象跟着改变
var a={
    name:1
}
var b=Object.assign({},a)
a.name=2;
console.log(b.name);        //1

//浅拷贝示例
var c={
    name:'aaa',
    type:{
        age:6
    }
}
var d=Object.assign({},c)
c.type.age=8;
console.log(d.type.age);   //8

Object.assign会把数组视为对象

var a=Object.assign([1, 2, 3], [4, 5]);
console.log(a);  //[ 4, 5, 3 ]


//步骤类似下方(但是不一样)
//1、
var a=Object.assign({},[1, 2, 3]);
console.log(a);  //{ '0': 1, '1': 2, '2': 3 }

//2、
var a=Object.assign({},[4,5]);
console.log(a);  //{ '0': 4, '1': 5 }

//3、
var a=Object.assign({ '0': 1, '1': 2, '2': 3 }, { '0': 4, '1': 5 });
console.log(a);     //{ '0': 4, '1': 5, '2': 3 }

取值函数(get)的处理,如果要复制的值是一个取值函数,那么将求值后再复制

Object.assign只能进行值的复制,如果要复制的值是一个取值函数,那么将求值后再复制。

const source = {
  get foo() { return 1 }
};
const target = {};

Object.assign(target, source)
// { foo: 1 }

常见用法

(1)加属性

x属性和y属性添加到Point类的对象实例

class Point {
  constructor(x, y) {
    Object.assign(this, {x, y});
  }
}
(2)添加方法

使用assign将方法添加到SomeClass.prototype之中

Object.assign(SomeClass.prototype, {
  someMethod(arg1, arg2) {
    ···
  },
  anotherMethod() {
    ···
  }
});

// 等同于下面的写法
SomeClass.prototype.someMethod = function (arg1, arg2) {
  ···
};
SomeClass.prototype.anotherMethod = function () {
  ···
};
(3)克隆对象(浅克隆),不能克隆它继承的值

将原始对象拷贝到一个空对象,就得到了原始对象的克隆。

只能克隆原始对象自身的值,不能克隆它继承的值

function clone(origin) {
  return Object.assign({}, origin);
}

//保持继承链的克隆
function clone(origin) {
  let originProto = Object.getPrototypeOf(origin);
  return Object.assign(Object.create(originProto), origin);
}
(4)合并多个对象

将多个对象合并到某个对象

//现有对象合并
const merge =(target, ...sources) => Object.assign(target, ...sources);


//合并到新对象
const merge = (...sources) => Object.assign({}, ...sources);
(5)为属性指定默认值

DEFAULTS对象是默认值,options对象是用户提供的参数。Object.assign方法将DEFAULTSoptions合并成一个新对象,如果两者有同名属性,则options的属性值会覆盖DEFAULTS的属性值

由于存在浅拷贝的问题,DEFAULTS对象和options对象的所有属性的值,最好都是简单类型

const DEFAULTS = {
  logLevel: 0,
  outputFormat: 'html'
};

function processContent(options) {
  options = Object.assign({}, DEFAULTS, options);
  console.log(options);
  // ...
}

Object.create(proto,propertiesObject),创建一个新对象,用现有的proto对象 当做新创建的对象的__proto__

参数

proto

新创建对象的原型对象

propertiesObject

可选。如果没有指定为 undefined,有则是要添加到新创建对象的可枚举属性(即其自身定义的属性,而不是其原型链上的枚举属性)对象的属性描述符以及相应的属性名称。这些属性对应Object.defineProperties()的第二个参数。

propertiesObject参数必须是null 或一个对象,否则则抛出一个 TypeError 异常

返回值

一个新对象,带着指定的原型对象和属性

用 Object.create实现类式继承

function Shape() {
    this.x=0;
    this.y=0;
}

Shape.prototype.move=function(x,y){
    this.x=x;
    this.y=y;
    console.info('Shape moved.');
}
function Rectangle() {
    Shape.call(this); // call super constructor.
}

Rectangle.prototype=Object.create(Shape.prototype);
// 混合其它prototype,继承到多个对象
//Object.assign(Rectangle.prototype, OtherSuperClass.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();

console.log(rect instanceof Rectangle); // true
console.log(rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'

Object.create 的 propertyObject参数的使用

默认创建的属性是不可写,不可枚举,不可配置的

var o;

// 创建一个原型为null的空对象
o = Object.create(null);


o = {};
// 以字面量方式创建的空对象就相当于:
o = Object.create(Object.prototype);


o = Object.create(Object.prototype, {
    // foo会成为所创建对象的数据属性
    foo: {
        writable:true,
        configurable:true,
        value: "hello"
    },
    // bar会成为所创建对象的访问器属性
    bar: {
        configurable: false,
        get: function() { return 10 },
        set: function(value) {
            console.log("Setting `o.bar` to", value);
        }
    }
});


function Constructor(){}
o = new Constructor();
// 上面的一句就相当于:
o = Object.create(Constructor.prototype);
// 当然,如果在Constructor函数中有一些初始化代码,Object.create不能执行那些代码


// 创建一个以另一个空对象为原型,且拥有一个属性p的对象
o = Object.create({}, { p: { value: 42 } })

// 省略了的属性特性默认为false,所以属性p是不可写,不可枚举,不可配置的:
o.p = 24
o.p
//42

o.q = 12
for (var prop in o) {
    console.log(prop)
}
//"q"

delete o.p
//false

//创建一个可写的,可枚举的,可配置的属性p
o2 = Object.create({}, {
    p: {
        value: 42,
        writable: true,
        enumerable: true,
        configurable: true
    }
});

Object.defineProperties(obj, props),在一个对象上新增属性或修改现有属性

在一个对象上新增属性或修改现有属性,并返回该对象。

参数

obj

在其上定义或修改属性的对象

props

新增的属性修改的属性属性描述符的对象。对象中存在的属性描述符主要有两种:数据描述符和访问器描述符(与Object.defineProperty() 的一样)

新增或修改属性必须带属性描述:configurable、value、writable、get、set

返回值

作为第一个参数的对象

var obj = {};
Object.defineProperties(obj, {
  'property1': {
    value: true,
    writable: true
  },
  'property2': {
    value: 'Hello',
    writable: false
  }
  // etc. etc.
});

console.log(obj.property1); //true

Object.getOwnPropertyDescriptors(obj)

返回指定对象所有自身属性(非继承属性)的描述对象

const obj = {
  foo: 123,
  get bar() { return 'abc' }
};

Object.getOwnPropertyDescriptors(obj)
// { foo:
//    { value: 123,
//      writable: true,
//      enumerable: true,
//      configurable: true },
//   bar:
//    { get: [Function: get bar],
//      set: undefined,
//      enumerable: true,
//      configurable: true } }

ES5 的Object.getOwnPropertyDescriptor()相似:返回某个对象属性的描述对象(descriptor) 该方法的引入目的,主要是为了解决Object.assign()无法正确拷贝get属性和set属性的问题。

Object.assign()无法正确拷贝get属性和set属性的问题:

const source = {
  set foo(value) {
    console.log(value);
  }
};

const target1 = {};
Object.assign(target1, source);

Object.getOwnPropertyDescriptor(target1, 'foo')
// { value: undefined,
//   writable: true,
//   enumerable: true,
//   configurable: true }

利用getOwnPropertyDescriptor解决:

const source = {
  set foo(value) {
    console.log(value);
  }
};

const target2 = {};

Object.defineProperties(target2, Object.getOwnPropertyDescriptors(source));
Object.getOwnPropertyDescriptor(target2, 'foo')
// { get: undefined,
//   set: [Function: set foo],
//   enumerable: true,
//   configurable: true 
// }

Object.setPrototypeOf(obj2,obj1),设置obj2的原型(__proto__)为obj1

设置obj2的原型(__proto__)为obj1

let obj1={name:'a'}
let obj2={}

Object.setPrototypeOf(obj2,obj1)
console.log(obj2);			//{}
console.log(obj2.name);		//a

//原理
obj2.__proto__=obj1;

如果第一个参数不是对象,会自动转为对象

第一个参数不是对象,会自动转为对象。但是由于返回的还是第一个参数,所以这个操作不会产生任何效果

Object.setPrototypeOf(1, {}) === 1 // true
Object.setPrototypeOf('foo', {}) === 'foo' // true
Object.setPrototypeOf(true, {}) === true // true

如果第一个参数是undefined或null会报错

由于undefined和null无法转为对象,所以如果第一个参数是undefined或null,就会报错

Object.setPrototypeOf(undefined, {})
// TypeError: Object.setPrototypeOf called on null or undefined

Object.setPrototypeOf(null, {})
// TypeError: Object.setPrototypeOf called on null or undefined

Object.getPrototypeOf(obj),取一个对象的原型对象

取一个对象的原型对象

function Rectangle() {
  // ...
}

const rec = new Rectangle();

Object.getPrototypeOf(rec) === Rectangle.prototype
// true

Object.setPrototypeOf(rec, Object.prototype);
Object.getPrototypeOf(rec) === Rectangle.prototype
// false

Object.keys(obj),所有可遍历(enumerable)属性的键名组成的数组

obj自身的(不含继承的)所有可遍历(enumerable)属性的键名 组成的数组

var obj = { foo: 'bar', baz: 42 };
Object.keys(obj)
// ["foo", "baz"]

Object.values(obj),所有可遍历(enumerable)属性的键值组成的数组

obj自身的(不含继承的)所有可遍历(enumerable)属性的键值 组成的数组

const obj = { foo: 'bar', baz: 42 };
Object.values(obj)
// ["bar", 42]

//只返回对象自身的可遍历属性
const obj = Object.create({}, {p: {value: 42}});
Object.values(obj) // []

如果Object.values方法的参数是一个字符串,会返回各个字符组成的一个数组

Object.values('foo')
// ['f', 'o', 'o']

Object.values会过滤属性名为 Symbol 值的属性

Object.values({ [Symbol()]: 123, foo: 'abc' });
// ['abc']

Object.entries(obj),可遍历(enumerable)属性的键值对组成的数组

obj自身的(不含继承的)所有可遍历(enumerable)属性的键值对 组成的数组

const obj = { foo: 'bar', baz: 42 };
Object.entries(obj)
// [ ["foo", "bar"], ["baz", 42] ]

Object.fromEntries(),将一个键值对数组转为对象

是Object.entries()的逆操作,用于将一个键值对数组转为对象。

特别适合将 Map 结构转为对象。

Object.fromEntries([
  ['foo', 'bar'],
  ['baz', 42]
])
// { foo: "bar", baz: 42 }