1.Object.is
Function type:
(v1:any,v2:any)=>boolean;
//接收两个用于比较是否相等的值,返回一个布尔值结果
The comparison rules of the Object.is(...)
===
, but there are two special values- NaN
and -0
, you need to pay attention to:
When performing a strict equality comparison, NaN
itself is not equal to , +0
and -0
are equal to :
NaN===NaN;//false
0===-0;//true
However Object.is(...)
determination rule in, NaN
is equal of, +0
and -0
are unequal (because +
, -
may represent the direction, -
in the general sense of the code):
Object.is(NaN,NaN);//true
Object.is(0,-0);//false
It is precisely because of this that when we need to strictly judge NaN
and -0
, we can use Object.is(...)
, this function does not replace ===
.
2.Object.getOwnPropertySymbols
Function type:
(obj:any)=>Symbol[];
//接受一个对象,返回这个对象中所有Symbol类型的键组成的数组,不存在Symbol属性时,返回空数组
ES6 There is a new basic type Symbol
, due Symbol
value unique characteristics, use Symbol
property as the object is very attractive. Object.getOwnPropertySymbols(...)
method is to get all the Symbol
attributes on the object.
const obj = {
[Symbol('prop')]: '我是symbol属性的值',
[Symbol.iterator]() {
return this;
},
};
console.log(Object.getOwnPropertySymbols(obj)); // [ Symbol(prop), Symbol(Symbol.iterator) ]
3.Object.setPrototypeOf
Function type:
<T>(obj:T,prototype:Object|null)=>T;
//该函数会将prototype设为obj的原型对象,返回值为obj自身
Compared to modifying the prototype object obj.__proto__ = prototype
__proto__
attribute, Object.setPrototypeOf(obj,protorype)
is more standardized to use 0618e3b319c637, so when we need to modify the prototype object, it is best to use Object.setPrototypeOf(...)
.
But it should be noted that the operation of modifying the prototype object of an object is very slow, and this behavior will also make the code more difficult to understand. It is best for us to avoid modifying the prototype object of the object. You can create an object with the prototype object we want Object.create(...)
const obj = {};
const obj2 = {
name: 'obj2',
};
//尽量避免
Object.setPrototypeOf(obj, obj2);
//稍微推荐
const obj3 = Object.create(obj2);
console.log(obj.name, obj3.name);//obj2 obj2
4.Object.assign
Function type:
<T,U>(target:T,...objs:U[])=>T&U;
//将所有可枚举属性的值从一个或多个源对象分配到目标对象,返回目标对象target
Object.assgin(...)
is the target object, and the other parameters are the data source objects whose attributes are to be copied. The function of the incoming data source object in accordance with the order, the order data source object enumerable and their own properties (on a non-prototype chain), shallow copy on to the target object.
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);
//name属性是obj的原型对象obj2上的属性,obj的notEnumerable是不可枚举的,所以最终只复制了一个self属性
console.log(target);//{ self: 'self' }
It should be noted that when the data source object passed in is null
or undefined
, this function will not throw an error.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。