What attributes are added to ES6 objects

What attributes are added to ES6 objects

Allows object attributes to be variables

let name = "Ken"

let age = 18

let obj = {name, age}

Copy the code

The method can be abbreviated

let person = {

  sayHi() {

    console.log("halou")

  }

}

Copy the code

Attribute expression

let person = {

  ['say'+'Hi'] () {

    console.log("sayHi")

  }

}



const hello = "Hello"

const obj = {

 [hello+"2"] :"world"

}

Copy the code

Object array extension operator

let obj = {name'Life Code'.age18}



letsomeone = {... obj}

Copy the code

Merge objects

let one = {name'Life Code'}

let two = {age18}



letsomeone = {... one, ... two}

Copy the code

Object

Object.assign

Use to copy all enumerable properties of the source object into the target object.

  • If the target object and the source object have an attribute with the same name, or if multiple source objects have an attribute with the same name, the later attribute overrides the previous one.
  • If the function has only one argument, the object is returned if the argument is an object. When the parameter is not an object, it is first converted to an object and then returned.
let target = {name"Life code"}

let obj1 = {age18}

let obj2 = {sex'male'}



Object.assign(target, obj1, obj2)



// target: {name: 'life code ', age: 18, sex:' male '}

Copy the code
  • Null and undefined cannot be converted to objects, so an error is reported:
Object.assign(null);       // TypeError: Cannot convert undefined or null to object

Object.assign(undefined);  // TypeError: Cannot convert undefined or null to object

When there is more than one parameter,null 和 undefinedIf the first object is not put, that is, the target object is not skippednull 和 undefined, not an error

Object.assign(1.undefined);  // Number {1}

Object.assign({a1},null);  // {a: 1}

 

Object.assign(undefined, {a1});  // TypeError: Cannot convert undefined or null to object

Copy the code

Pay attention to the point

Assign copies are shallow copies:

let sourceObj = { a: { b1}};

let targetObj = {c3};

Object.assign(targetObj, sourceObj);

targetObj.a.b = 2;

sourceObj.a.b;  / / 2

Copy the code

Attribute substitution with the same name:

targetObj = { a: { b1.c:2}};

sourceObj = { a: { b"hh"}};

Object.assign(targetObj, sourceObj);

targetObj;  // {a: {b: "hh"}}

Copy the code

Array handling:

The array is processed as an object, so [2,3] is converted to {0:2,1:3} before the property is copied, so the 0 attribute of the source object overwrites the 0 of the target object.

Object.assign([2.3], [5]);  / / (5, 3]

Copy the code

Object.is(value1, value2)

Used to compare whether two values are strictly equal, similar to (===).

Object.is("q"."q");      // true

Object.is(1.1);          // true

Object.is([1], [1]);      // false

Object.is({q:1}, {q:1});  // false

Copy the code

And (===)

+0 does not equal -0

Object.is(+0.0);  //false

+0= = =0  //true

// NaN is equal to itself

Object.is(NaN.NaN); //true

NaN= = =NaN  //false

Copy the code