Deep understanding of properties and features in JavaScript

Source: Internet
Author: User

In-depth understanding of properties and features in JavaScript?

The properties and attributes in JavaScript are two completely different concepts, and I'll take a closer look at the properties and features in JavaScript, based on what I've learned.

The main contents are as follows:

    • Understanding the nature of objects in JavaScript, understanding the relationship between objects and classes, and the relationship between objects and reference types
    • How object properties are categorized
    • Understanding of attributes in a property

Part I: Understanding the nature of objects in JavaScript, understanding the relationship between objects and classes, and the relationship between objects and reference types

The essence of an object:ECMA-262 defines an object as a collection of unordered properties whose properties can contain basic values, objects, or functions . That is, an object is a set of values that have no specific order, and each property or method of an object has a name that maps to a value. The essence of an object is a hash table: a set of name-value pairs, which can be data or functions. 、

Relationships between objects and classes: in JavaScript, objects and classes have no relationship . This is because there is no concept of class in ECMAScript, and its objects are different from those in other class-based languages .

  object and reference type relationships: objects and reference types are not equivalent because each object is created from a reference type .

Part II: How object properties are categorized

Objects created by constructors or object literal methods have properties and methods (as long as they refer to properties and methods, they must belong to the object; As long as the object is mentioned, it must be a property and a method (except for customization), where the attributes can be divided into data properties and accessor properties, and their differences are as follows:

    • Data properties are typically used to store data values, and accessor properties do not contain a data value
    • Accessor properties are used for get/set operations

Part III: Understanding of attributes in Properties

ECMAScript defines the concept of attributes (attribute) in order to describe the various characteristics of object properties. That is, attributes are different from attributes, and attributes are used to describe properties. Below, I will explain separately:

    • Data properties and their characteristics
    • accessor properties and their attributes
    • How to define multiple attributes using the Object.defineproperties () method
    • How to use the Object.getownpropertydescripter () method to read a property's descriptor to read the attributes of a property

1. Data attributes and their characteristics

  As we've just said, data properties are used to store data values, so the Data property has a location for the data value, where the value can be read and written. The data attribute has 4 attributes that describe its behavior, because ECMAScript rules that the attribute cannot be accessed directly in JavaScript (note: not inaccessible), so we put it in two sets of square brackets. As follows:

    • [[[Configurable]]: The default value is True,a, indicating whether the Delete property can be removed by deleting to redefine the property B, can modify the property's attribute C, the ability to modify the property from the data property to the accessor property
    • [[[Enumerable]]: The default value is True, indicating whether the property can be returned through a for-in loop (so: If False, the for-in loop cannot enumerate the property it is in)
    • [[[Writable]]: The default value is True, which indicates whether the value of the property can be modified, which differs from [[configurable]].
    • [[value]]: The default value is undefined, which is the attribute value of the property, we can read the property value at this location, you can also write the property value at this location.
    • Note: The default above refers to properties owned by objects created by constructors or object literals, rather than the Object.defineproperty () method described below

These attributes all have default values, but what if these defaults are not what we want? Of course it's a change! We can use the Object.defineproperty () method to modify the property's default attributes. The English difineproperty is defined as the meaning of the attribute. This method receives three parameters: the object where the property resides, the name of the property, and a descriptor object. where the third parameter descriptor object is created by the method of the object literal, the attributes and property values inside are actually saved with the attributes and attribute values to be modified.

Here are a few examples for a deeper understanding.


                var person={};object.defineproperty (person, "name", {writable:false,value: "Zhuzhenwei"}); Console.log ( ;//Zhu Zhenwei "heting"; Console.log (;//Zhu Zhenwei    

Here I created an object using the object literal method, but I didn't create the method and the property at the same time. Instead, the Object.defineproperty () method was used to create the properties and modify the default values. This sets the writable to false, which is not valid when I try to modify later.


                var person={};object.defineproperty (person, "name", {value: "Zhuzhenwei"}); Console.log (;// "heting"; Console.log (;//zhuzhenwei

  Pay attention to this example, I deleted the writable:false in this example, why still can not modify it? This is because the first three defaults to ture when I introduced the feature, which is obtained with the creation of the object and the creation of the property. For properties created by calling the Object.defineproperty () method, the default value for the first three attributes is false, which you need to be aware of.



In-depth understanding of properties and features in JavaScript

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: 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.