Skip to main content

Similarities and differences between Object.defineProperty and Object.defineProperties of js

 The Object.defineProperty() method defines a new property directly on an object, or modifies an existing property of an object, and returns the object. Contains three parameters: the object to be manipulated, the specified attribute of the manipulated object, and the attribute descriptor to be defined or modified. The last parameter is an object. You can specify its value, enumerable: control whether the property can be enumerated, the default value is false. writable: Controls whether the property can be modified, the default value is false. configurable: Controls whether the property can be deleted, and whether other properties except the value and writable properties can be modified. The default value is false.get: Called when the value of this property is obtained. set: Called when the value of this property is overridden.

Core points:

  • The configurable attribute indicates whether an object's properties can be deleted, and whether other attributes other than the value and writable attributes can be modified. When it is set to false for the first time, it is not possible to rewrite it. The attribute value also cannot be deleted and modified.
  • The configurable attribute indicates whether an object's properties can be deleted, and whether other attributes other than the value and writable attributes can be modified. When it is set to false for the first time, it is not possible to rewrite it. The attribute value also cannot be deleted and modified.

        var obj = {};
        Object.defineProperty(obj,"hobby",{
            value:"swim",
            enumerable:true,
            writable:false,
            configurable:false
        })
        Object.defineProperty(obj,"hobby",{
            value:"sing",
            // configurable:true,//err
            writable:false
        })
        console.log(obj.hobby)

The following are the default values in the two defined cases, pay special attention to

var oobj = {};
 //One
 obj.hobby = "swim";
 // The default values are as follows::
 Object.defineProperty(obj, "hobby", {
   value: swim,
   writable: true,
   configurable: true,
   enumerable: true
 });
 
 
 // Two
 Object.defineProperty(obj, "hobby", { value : "sing" });
 // The default values are as follows:
 Object.defineProperty(obj, "hobby", {
   value: "sing",
   writable: false,
   configurable: false,
   enumerable: false
 })

To read the property value of the property, use Object.getOwnPropertyDescriptor

        var obj = {};
        Object.defineProperty(obj,"hobby",{
            value:"swim",
            enumerable:true,
            writable:false,
            configurable:false
        })
        console.log(Object.getOwnPropertyDescriptor(obj, "hobby"))

When we want to set multiple properties for an object, we need to use the Object.defineProperties() method. The specific usage is similar.

Comments

Popular posts from this blog

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.