Object 的一些静态方法

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 static Reflect.deleteProperty() method allows to delete properties. It is like the delete operator as a function.
  • Reflect.defineProperty()
    The static Reflect.defineProperty() method is like Object.defineProperty() but returns a Boolean.
  • Reflect.getOwnPropertyDescriptor()
    The static Reflect.getOwnPropertyDescriptor() method is similar to Object.getOwnPropertyDescriptor(). It returns a property descriptor of the given property if it exists on the object, undefined otherwise.
  • Reflect.getPrototypeOf()
    The static Reflect.getPrototypeOf() method is almost the same method as Object.getPrototypeOf(). It returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.
  • Reflect.has()
    The static Reflect.has() method works like the in operator as a function.
  • Reflect.ownKeys()
    The static Reflect.ownKeys() method returns an array of the target object's own property keys.
  • Reflect.setPrototypeOf()
    The static Reflect.setPrototypeOf() method is the same method as Object.setPrototypeOf(), except for its return type. It sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or to null, and returns true if the operation was successful, or false otherwise.

Reflect.setPrototypeOf() and Object.setPrototypeOf()

Reflect.setPrototypeOf() is same as Object.setPrototypeOf().

typescript
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 the target 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 object obj. The ordering of the enumerable properties in the array is consistent with the ordering exposed by a for...in loop (or by Object.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 to Object.getOwnPropertyNames(), you can get all symbol properties of a given object as an array of symbols. Note that Object.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 upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

  js就是个垃圾,我也是个垃圾