Object 的一些静态方法
Published by powerfulyang on Aug 1, 2022
Object.preventExtensions() 和 Reflect.preventExtensions()
new properties cannot be added, existing properties can be removed Object.preventExtensions() 与 Reflect.preventExtensions() 的不同点 如果 Reflect.preventExtensions() 的 target 参数不是一个对象(是原始值),那么将造成一个 TypeError 异常。 对于 Object.preventExtensions() 方法, 非对象的 target 参数将被强制转换为对象。
Object.freeze()
Freezing an object prevents extensions and makes existing properties non-writable and non-configurable.
A frozen object can no longer be changed: new properties cannot be added, existing properties cannot be removed, their enumerability, configurability, writability, or value cannot be changed, and the object's prototype cannot be re-assigned. freeze()
returns the same object that was passed in.
Object.seal()
Sealing an object prevents extensions and makes existing properties non-configurable.
A sealed object has a fixed set of properties: new properties cannot be added, existing properties cannot be removed, their enumerability and configurability cannot be changed, and its prototype cannot be re-assigned. Values of existing properties can still be changed as long as they are writable. seal()
returns the same object that was passed in.
Reflect 和 Object 的一些静态方法
- Reflect.deleteProperty()
The staticReflect.deleteProperty()
method allows to delete properties. It is like thedelete
operator as a function. - Reflect.defineProperty()
The staticReflect.defineProperty()
method is likeObject.defineProperty()
but returns aBoolean
. - Reflect.getOwnPropertyDescriptor()
The staticReflect.getOwnPropertyDescriptor()
method is similar toObject.getOwnPropertyDescriptor()
. It returns a property descriptor of the given property if it exists on the object,undefined
otherwise. - Reflect.getPrototypeOf()
The staticReflect.getPrototypeOf()
method is almost the same method asObject.getPrototypeOf()
. It returns the prototype (i.e. the value of the internal[[Prototype]]
property) of the specified object. - Reflect.has()
The staticReflect.has()
method works like thein
operator as a function. - Reflect.ownKeys()
The staticReflect.ownKeys()
method returns an array of thetarget
object's own property keys. - Reflect.setPrototypeOf()
The staticReflect.setPrototypeOf()
method is the same method asObject.setPrototypeOf()
, except for its return type. It sets the prototype (i.e., the internal[[Prototype]]
property) of a specified object to another object or tonull
, and returnstrue
if the operation was successful, orfalse
otherwise.
Reflect.setPrototypeOf() and Object.setPrototypeOf()
Reflect.setPrototypeOf() is same as Object.setPrototypeOf().
1 2 3 4 5 6 7 8 9 10
class Human {} class SuperHero {} // Set the instance properties Object.setPrototypeOf(SuperHero.prototype, Human.prototype); // Hook up the static properties Object.setPrototypeOf(SuperHero, Human); const superMan = new SuperHero();
Reflect.ownKeys() and Object.keys()
- The
Reflect.ownKeys
method returns an array of thetarget
object's own property keys. Its return value is equivalent to Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target)).Object.getOwnPropertyNames()
returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly in a given objectobj
. The ordering of the enumerable properties in the array is consistent with the ordering exposed by afor...in
loop (or byObject.keys()
) over the properties of the object. According to ES6, the non-negative integer keys of the object (both enumerable and non-enumerable) are added in ascending order to the array first, followed by the string keys in the order of insertion.Object.getOwnPropertySymbols()
is similar toObject.getOwnPropertyNames()
, you can get all symbol properties of a given object as an array of symbols. Note thatObject.getOwnPropertyNames()
itself does not contain the symbol properties of an object and only the string properties.
Object.keys()
returns an array whose elements are strings corresponding to the enumerable properties found directly uponobject
. The ordering of the properties is the same as that given by looping over the properties of the object manually.
js就是个垃圾,我也是个垃圾