Writing in the front

JS object method is no matter in the interview, or in the work to do the project, or is in the understanding of JS or JS framework principle is very important, this article summarizes the object method in JS in detail, let you do to their use of a net.

To eat: Primary front-end taste index: 😋😋😋😋😋

Object

1. Object.create

Object.create(proto, [propertiesObject]) : Creates an Object. The first parameter is the prototype of the new Object. The second parameter configures the properties of the new Object

let obj = Object.create({ a: 1 }, {
  b: {
    value:2./ / property values
    enumerable: true.// Whether enumerable
    writable: true.// Whether the value can be overridden
    configurable: true  // Check whether the preceding configurations can be modified}})console.log(obj)  //{ b: 2 }
console.log(obj.__proto__);  //{ a: 1 }
Copy the code

2. The Object. DefineProperty (obj, prop, descriptor) :

Object.defineproperty (obj, prop, Descriptor) : Define a new property or modify a property on a specified Object and configure it accordingly. The first parameter is the configured Object, the second parameter is the name of the property to be defined or modified, and the third attribute is the property descriptor

  • Value: indicates the value of an attribute
  • Writable: Defines whether it is rewritable. Default is false
  • Enumerable: Defines whether it is enumerable or not. Default is false
  • Different: Specifies whether the descriptor can be changed, and the default is false
  • Get: The getter function for the property. This function is executed when accessing the property. Default is undefine
  • Set: the setter function for a property that is called when the property is modified. The default is undefined

Attribute descriptors are divided into data descriptors and access descriptors

  • A data descriptor is a property with a value that can be writable or unwritable
  • Access descriptors are properties described by getter and setter functions. If a descriptor has both a value or writable and a GET or set key, an exception is raised.
let obj = {};
Object.defineProperty(obj, 'a', {
  value: 1.writable: true.// Whether the definition can be overridden
  enumerable: true.// Whether the definition is enumerable
  configurable: true.// Define whether it is configurable
})
Copy the code

3. Object.assign

Object.assign(target, … Sources) : Assigns enumerable properties of one or more objects to and returns the target object.

const target = {a: 1};
const obj = Object.assign(target, {b: 3}, {b: 4.c: 5});
console.log(obj);  //{a: 1, b: 4, c: 5} Note that the following source object attributes override the preceding ones
Copy the code

4. Object.freeze

Object. Freeze:

  • When an object is frozen, it cannot be modified
  • No new attributes can be added, no attributes can be deleted, no attribute descriptors can be modified, and no stereotypes can be modified.
  • But if the value of a property is an object, then the property in that object can be modified, unless it’s also a frozen object. Arrays are frozen objects, and their elements can’t be modified. No array elements can be added or removed.
let obj = {
  a: 1.object: {
    b: 5,}}Object.freeze(obj);
obj.a = 2;
console.log(obj.a);  / / 1
obj.object.b = 6
console.log(obj.object)  // { b: 6 }
Copy the code

5.Object.getOwnPropertyDescriptor

Object. GetOwnPropertyDescriptor: return to the specified Object on a has its own corresponding attribute descriptors, not on the prototype.

let obj = { a: 1 };
let des = Object.getOwnPropertyDescriptor(obj, 'a');
console.log(des.value);  / / 1
console.log(des.writable);// true
Copy the code

6.Object.getOwnPropertyNames

Object. GetOwnPropertyNames: return to specify the Object itself (not including prototype) all of the property name (including an enumerable and an enumeration) consisting of an array

let obj = {a:1.b:2};
let res = Object.getOwnPropertyNames(obj);
console.log(res);  // [ 'a', 'b' ]
Copy the code

7. Object.getPrototypeOf

Object.getPrototypeOf: Returns the prototype Object of the specified Object

let obj = {a: 1};
obj.__proto__ = {b:2};
console.log(Object.getPrototypeOf(obj));  // { b: 2 }
Copy the code

8. Object.is

Object.is: Compares whether two values are equal, the same as === except for NaN, +0, and -0

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

9. Object.seal

Object.seal: Seals an Object. No new attributes can be added and the current attributes are not configurable, but the writability of the attributes remains unchanged

let obj = { a: 1 };
Object.seal(obj);
obj.a = 2;
obj.b = 3;
console.log(obj.a);  / / 2
console.log(obj.b);  //undefined
Copy the code

10. Object.preventExtensions

Object.preventExtensions: Make an Object unextensible and cannot add new properties

let obj = { a: 1 };
Object.preventExtensions(obj);
obj.b = 2;
console.log(obj.b)  //undefined
Copy the code

11. Object.isExtensible

Object.isExtensible: determines whether an Object isExtensible

The object. preventExtensions, Object.seal, or Object.freeze methods can all mark an Object as unextensible

let obj = { a: 1 };
Object.freeze(obj);
obj.b = 2;
console.log(obj.b);  //undefined
Copy the code

12. Object.keys

Keys: Returns an array of enumerable properties of the Object itself (excluding stereotypes)

let obj = {
  name: 'jack'.age: 13.__proto__: {
    address: 'abc',}}console.log(Object.keys(obj))  //[ 'name', 'age' ]
Copy the code

13. Object.values

Object.values: Returns an array of enumerable property values of the Object itself (excluding stereotypes)

let obj = {
  name: 'jack'.age: 13.__proto__: {
    address: 'abc',}}console.log(Object.values(obj))  //[ 'jack', 13 ]
Copy the code

14. Object.entries

Object.entries: Returns an array of key-value pairs with enumerable properties of the Object itself (excluding stereotypes)

let obj = {
  name: 'jack'.age: 13.__proto__: {
    address: 'abc',}}console.log(Object.entries(obj))  //[ [ 'name', 'jack' ], [ 'age', 13 ] ]
Copy the code

Thank you for reading

Thank you very much for reading to the end, if there is any mistake, I hope you can point out, so as not to mislead others. If you think it is helpful to you, I hope you can click a like, add a concern, any question can contact me, I hope we can make progress together. Finally, I wish you a bright future, we climb each other, high meet 🌈!