Two types of properties of JavaScript objects (data properties and accessor properties)

Time:2022-3-21

For JavaScript, attributes are not just names and values. JavaScript uses a set of attributes to describe attributes.

The first type of attribute data attribute has four characteristics.

Value: is the value of the attribute.

Writable: determines whether the attribute can be assigned.

Enumerable: determines whether for in can enumerate the attribute.

Configurable: determines whether the attribute can be deleted or the characteristic value can be changed.

In most cases, we only care about the values of data attributes.

The second type of attribute is the accessor (getter / setter) attribute, which also has four characteristics.

Getter: function or undefined, which is called when getting the attribute value.

Setter: function or undefined, which is called when setting the property value.

Enumerable: determines whether for in can enumerate the attribute.

Configurable: determines whether the attribute can be deleted or the characteristic value can be changed.

The code we usually use to define attributes will generate data attributes,

Among them, writable, enumerable and configurable are all true by default.

We can use the built-in function object Getownpropertydescriptor to view,

As shown in the following code:

var o = { a: 1 }; o.b = 2; // Both a and B are data attributes

Object.getOwnPropertyDescriptor(o,”a”) // {value: 1, writable: true, enumerable: true, configurable: true}

Object.getOwnPropertyDescriptor(o,”b”) // {value: 2, writable: true, enumerable: true, configurable: true}

 

Here, two kinds of syntax are used to define the attribute. After defining the attribute, we use the JavaScript API to view the attribute,

We can find that the attributes defined in this way are data attributes, and the default values of writeable, enumerable and configurable are true.

 

If we want to change the characteristics of attributes or define accessor attributes, we can use object Defineproperty, for example:

var o = { a: 1 }; Object. defineProperty(o, “b”, {value: 2, writable: false, enumerable: false, configurable: true}); // Both a and B are data attributes, but the eigenvalues have changed

Object.getOwnPropertyDescriptor(o,”a”); // {value: 1, writable: true, enumerable: true, configurable: true}

Object.getOwnPropertyDescriptor(o,”b”); // {value: 2, writable: false, enumerable: false, configurable: true}

o.b = 3; console.log(o.b); // 2

Used object Define property to define the property, so defining the property can change the writable and enumerable of the property.

We also use object Getownpropertydescriptor to check and find that the writable and enumerable features are indeed changed. Because the writable feature is false, we re assign the value of B, and the value of B will not change.

When creating objects, you can also use the get and set keywords to create accessor properties. The code is as follows:

var o = { get a() { return 1 } };

console.log(o.a); // 1

The accessor property is different from the data property. The getter or setter function will be executed every time the property is accessed. Here, our getter function returns 1, so O.A gets 1 every time.

The runtime of a JavaScript object is a “collection of attributes”, and attributes appear as strings or symbols (ES6)Symbol type)Is key, and the characteristic value of data attribute or accessor attribute is value.

Now JS has seven major types: underfined, null, number, string, Boolean, object, and symbol.

Recommended Today

Graph Theory

Graph Theory Graph theory is a branch of mathematics. It takes pictures as research objects. A graph in graph theory is a graph composed of a number of given points and a line connecting two points. This kind of graph is usually used to describe a certain relationship between certain things. The lines of the […]