JavaScript series-----objects based on hash storage (<Key,Value> Value) (2)

Source: Internet
Author: User

JavaScript Series-----OBJECTJ Hash-based storage <Key,Value> Value

1. Questions raised

In the JavaScript series-----object based on hash<key,value> storage Key (1), we infer that the object is stored in a hash structure, and that the object's properties are represented as multiple <Key,Value> Key-value pairs.

Where the data type of key is a string, but we do not say what data structure is stored with value, in this article we will continue to discuss: thestorage type of value-----The core of the blog post

Since the properties of an object are stored as key-value pairs in JavaScript, we know that there are three types of object properties that need to be known:

      1. Properties of the base data type
      2. Properties of reference data types
      3. Data accessor Properties

Let's use an example to illustrate these three types of attributes:

varperson ={};p erson.name= ' Zhang San '; First, the basic data type property Person.age= 18;p Eron.getname=function() {///second, the property of the reference data type, because the function is also an object in JSreturn  This. Name; Object.defineproperty (person,' Isadult ', {//third, accessor Property Get:function () {    if(Person.age >= 18) {      return true; } Else {      return false;      }}); Console.log (Person.isadult); True

With the above example, we recognize three types of attributes, then let's talk about how value represents these three properties!

Data structure of Value in 2.<key,value>

In JavaScript Advanced Programming (Third Edition), this is the attribute: attributes are created with some eigenvalues that the JavaScript engine uses to define their behavior.

Below, we discuss the characteristics of each of the three attributes, respectively:

(1). Properties of the base data type

var person == ' Zhang San ';              var descriptor=object.getownpropertydescriptor (person, "name"); Console.log (descriptor);     Output Result:
Configurable True
Enumerable True
Value "Zhang San"
Writable True

(2). Properties of the reference data type

var person =function  () {  returnthis. Name;}
Person.child={name: "Zhang Four"}; var descriptor = object.getownpropertydescriptor (person, ' getName '); Console.log (descriptor); Output Result:
Configurable True
Enumerable True
Writable True
Value function ()
var descriptor1 = object.getownpropertydescriptor (person, ' child ');
Console.log (Descriptor1); Output Result:
                          
configurable &NBSP; true
enumerable   &NBSP; < Span class= "Objectbox objectbox-number" >true
value &NBSP; object {name=" Zhang Four "}
Writable True

(3). Accessor Type properties

 var  person = {};object.defineproperty ( person,  ' isadult '  function   () { if  (person.age >= 18 return  true  ;  else   { return  false  ; }  }}); var  descriptor=object.getownpropertydescriptor (person, "Isadult"  
configurabl E
&NBSP; false
enumerable &NBSP; false
set &NBSP; < Span class= "Objectbox objectbox-undefined" >undefined
Get function ()

From the above three examples can read, Value is also a pair of <Key,Value> representation of a structure. Of course, we can also consider value as an object because the object is inherently a struct.

So the answer to the question is obvious:

objects are stored in a hash structure, with the <Key,Value> key value pairs representing the properties of the object, the data type of key is a string, and the data type of value is struct, that is, the object is a <String,Object> The HASHMAP structure of the type is stored.

Meaning of the various characteristic values in 3.Value

(1) Characteristics of data types (basic data type properties and reference data type properties)

As we can see from the second section, there are four attribute values for the properties of the data type:

Characteristics Data type Significance
Configurable Boolean Indicates whether this property attribute can be modified, false, this property cannot be deleted (for delete), and cannot be modified
Enumerable Booelan Indicates whether this property can be enumerated, false, cannot be enumerated (for for-(in))
Writeable Boolean Indicates whether the value of the property can be modified to false when the value of this property cannot be modified
Value Set according to the specific value The data value of the property, where the new value is saved at the time the property is written

(2) attributes of accessor types (properties of accessor types)

Characteristics Data type Significance
Configurable Boolean Indicates whether this property attribute can be modified, false, this property cannot be deleted (for delete), and cannot be modified
Enumerable Booelan Indicates whether this property can be enumerated, false, cannot be enumerated (for for-(in))
Get function The value of the property is returned by the function
Set function Modify the value of the property, and the parameter is the value of the property.

a little extension (other attributes in value):

The Value in <Key,Value> is a struct, and does it only preserve the attributes enumerated above? ------of course not.

For example: In value that describes the four attributes of the data type, value can also represent an object, or a string, or a number, then there is a description of the type represented by value, except that the JS engine is processed in the background and we cannot see it.

of course, there must be another feature in the value of <Key,Vaule>, (base)-----Base is a reference type of data that points to the object that owns the property. When value is a function type, when the function is called, it is initialized with base to initialize the this pointer (which, of course, is beyond the scope of our discussion).

JavaScript series-----objects based on hash storage (<Key,Value> Value) (2)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.