Data attributes and storage attributes of JavaScript notes, javascript Memory
In javascript, object attributes are divided into data attributes and memory attributes:
Differences between the two attributes
Let's use Object. defineProperty () to intuitively understand the differences between the two.
You can use Object. defineProperty () to set data attributes as follows:
Var obj = {}; Object. defineProperty (obj, "prop", {value: 1, writable: true, // writeable enumerable: true, // enumerable retriable: true // sets whether the attribute can be deleted and whether the enumerable attribute can be modified })
You can use Object. defineProperty () to set the storage attributes as follows:
Var obj = {}; Object. defineProperty (obj, "prop", {getsetenumerable: true, // enumerable retriable: true // sets whether the attribute can be deleted and whether the enumerable attribute can be modified })
From the above example, we can see that the Memory attribute does not have the two attributes value and writable, instead of the set and get attributes.
Storage Properties
After reading the differences between the data attributes and the storage attributes, let's take a closer look at the attributes of the storage attributes that are easily ignored (that is, my TT.
The biggest difference between the storage attribute and the data attribute is the addition of getter/setter, which can be used to operate the attribute values to implement some practical functions.
// Example1function serialnum () {var n = 1; var prop = null; Object. defineProperty (this, "n", {get: function () {return n ;}, set: function (value) {if (value> n) n = value; else throw' enter a value greater than n ';}})} var obj = new serialnum (); obj. n = 2; // 2obj. n = 0; // enter a value greater than n for Uncaught.
In the preceding example, the set function is used to control the value range of n.
For details about the attributes stored in js data attributes, I would like to introduce them here and hope to help you!
Articles you may be interested in:
- Question about jQuery data api when custom data attributes are strings in json format
- Jsoup obtains the data attribute values of all regions (provinces, cities, counties, towns, and villages)