Data attributes and storage attributes of JavaScript notes, javascript Memory

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.