1.Object.is

Function type:

(v1:any.v2:any) = >boolean;
// Receives two values used to compare whether they are equal, returning a Boolean result
Copy the code

Object.is(…) Function comparison rules are basically the same as strict equality ===, but there are two special values — NaN and -0, which need to be noted:

NaN itself is not equal when strictly equal comparisons are made, and +0 and -0 are equal:

NaN= = =NaN;//false
0= = = -0;//true
Copy the code

But in Object. Is (…). NaN is equal and +0 and -0 are not equal (because +, – can represent direction and – generally makes sense in code) :

Object.is(NaN.NaN);//true
Object.is(0, -0);//false
Copy the code

This is why we can use object.is (…) when we need to be strict about NaN and -0. This function does not replace === =.

2.Object.getOwnPropertySymbols

Function type:

(obj:any) = >Symbol[];
// Take an object and return an array of all Symbol keys in the object, or an empty array if no Symbol attribute exists
Copy the code

ES6 adds a new base type Symbol, which is attractive to use as an object attribute due to the unique nature of its value. Object.getOwnPropertySymbols(…) This method is used to get all the Symbol properties on the object.

const obj = {
    [Symbol('prop')]: 'I am the value of the symbol property'[Symbol.iterator]() {
        return this; }};console.log(Object.getOwnPropertySymbols(obj)); // [ Symbol(prop), Symbol(Symbol.iterator) ]
Copy the code

3.Object.setPrototypeOf

Function type:

<T>(obj:T,prototype:Object|null) = >T;
// This function sets prototype to obj and returns obj itself
Copy the code

Object.setprototypeof (obj,protorype) is more canonical than modifying the prototype Object obj.__proto__ = prototype via the __proto__ attribute, so when we need to modify the prototype Object, Better to use Object.setPrototypeof (…) .

However, it is important to note that modifying an object’s prototype object is slow, and this behavior can make the code more difficult to understand. It is best to avoid modifying the Object’s prototype Object by using object.create (…). Create an object with the desired prototype object.

const obj = {};
const obj2 = {
    name: 'obj2'};// Try to avoid it
Object.setPrototypeOf(obj, obj2);
// Slightly recommended
const obj3 = Object.create(obj2);
console.log(obj.name, obj3.name);//obj2 obj2
Copy the code

4.Object.assign

Function type:

<T,U>(target:T,... objs:U[]) = >T&U;
// Assigns the values of all enumerable attributes from one or more source objects to the target object, returning the target object target
Copy the code

Object.assgin(…) The first argument to is the target object, and the other arguments are the data source object to copy the properties from. This function shallow copies enumerable and self-owned (not on the stereotype chain) properties from the data source object to the target object in the order in which they are passed in.

const obj = {
    self: 'self'};const obj2 = {
    name: 'obj2'};Object.setPrototypeOf(obj, obj2);
Object.defineProperty(obj, 'notEnumerable', {
    enumerable: false});const target = {};
Object.assign(target, null.undefined, obj);
// the name attribute is an attribute on obj's prototype object obj2. Obj's notEnumerable is notEnumerable, so it just copies self
console.log(target);//{ self: 'self' }
Copy the code

Note that this function does not throw an error when the data source object is passed null or undefined.