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

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.