Data properties and accessor properties in ECMAScript 5

Source: Internet
Author: User

Brief introduction

There are two special properties in the object defined by ECMAScript, which are given when you define an object's properties, and we can rewrite the two special properties to rationalize the access to their properties when necessary, and the two special properties address and function as follows:

  1. Data properties: By setting the properties of the data, we can allow the user to manipulate normal data (Obj.name) to restrict the permissions they can manipulate, make their data non-modifiable, not be enumerated in for...in, not be able to delete the data and not allow it to change the original partial data properties ([ [Enumerable]], [[configurable]]) and so on.

  2. Accessor properties:

In general, it is a set of properties in an object, such as when you do not want to make this property public, you can use the above two special properties.

Data properties

First, let's look at the specific characteristics of the data properties:

Describe attributes
Data type
Describe
Default value
[[Value]]
Any type of ECMAScript
The data value that contains this property
Undefined
[[Writable]]
Boolean
If the value is false, the [[Value]] attribute of the property cannot be changed
False
[[Enumerable]] Boolean If the value is true, the property can be enumerated using the for...in loop
False
[[Configurable]] Boolean If the value is false, the property cannot be deleted and the attribute cannot be configured again. False

Here's a brief introduction to what we'll use in the following practice:

    1. Object.defineproperty: Defines a new property directly on an object, or modifies an already existing property and returns the object.

    2. Object.getownpropertydescriptor: Returns the property descriptor corresponding to a property on the specified object. (own attribute refers to a property that is directly assigned to the object and does not need to be looked up from the prototype chain)

First, let's look at what the data attributes are:

Here we use the object.getownpropertydescriptor to get the data property of the Fun property I defined, and I can see that the property name in the returned object is the specific attribute name of the Data property, so let's practice the effect of setting the Data property.

Here we use the Object.defineproperty method to set:

It is obvious here that when we set the Data property [[Value]] to "static" in obj, the fun value in obj is a function, but it has now been changed to a "static" string.

Since we have rewritten the Obj.fun data property [[writable]] in the above, it is not written successfully to overwrite the fun property here.

When we try to delete the Obj.fun property, return False here, and then we want to change the value of [[Enumerable] of the fun property again, we will get an error, which is the result after we modify [[configurable]].

This is the result of our modification [[Enumerable]] when we try to enumerate the property list of obj. If you want to enumerate all the property names of an object, it can be obtained using the Object.getownpropertynames method, which is not shown here.

Finally, the Object.getownpropertydescriptor method is used to view the data properties of the fun, which has become the

Accessor properties
Describe attributes Data type Describe
Default value
[[Get]]
function object or undefined
The function uses an empty argument list to read the property values in the case of permission access. See also get. Undefined
[[Set]] function object or undefined
The function has a parameter that is used to write the property value, see also set Undefined

There are two other properties, and the data attribute is a dime, the only difference is that at this point [[configurable]] represents the ability to transform into a data property.

The definition of accessor properties is also done through Object.defineproperty, and is used in the same way as the definition of data properties, except that value and writable become get and set, and I don't want to make meaningless practices, What I'm going to say is that I just didn't understand. Access its properties and data properties.

At last

In the middle of writing this article, my understanding of data properties and accessor properties has gone the opposite way with the original meaning, which has always been to assume that these two properties are an accessory to object properties (Obj.fun), just like enchanted cards. This creates confusion when I use the same method to define two different descriptions of object property settings, one that I already have data properties and accessor properties that can coexist, like magic cards that can be enchanted, but when you see the description of accessor properties and data properties, there are two seemingly identical descriptions, Let me not understand.

The answer to this question suddenly dawned on the fact that accessor properties and data properties are a way of defining an intrinsic form of an object's properties, which is a method of making object properties like an AP from AD, which alters the representation of an object's properties, not the additional attributes. The strict theory is that the properties of an object have two mutually unrelated representations. I think a lot of readers here also understand, I am stupid, deep sleep This problem interesting also, so written in the last.



From for notes (Wiz)

Data properties and accessor properties in ECMAScript 5

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.