Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are property descriptor objects stored?

I know that you can obtain a property descriptor object of a certain property 'prop' of a certain object obj with Object.getOwnPropertyDescriptor(obj,"prop");. I was just wondering: Where are these objects stored? Are they stored internally within an object or .... elsewhere? I tried to find them in developer tools but with no luck.

like image 306
Danield Avatar asked Nov 24 '15 09:11

Danield


People also ask

What is a property descriptor in JavaScript?

A property descriptor is a simple JavaScript object associated with each property of the object that contains information about that property such as its value and other meta-data. In the above example, we have created a plain JavaScript object myObj using literal syntax with myPropOne and myPropTwo properties.

How to get all property descriptors of an object at once?

To get all property descriptors at once, we can use the method Object.getOwnPropertyDescriptors (obj). Together with Object.defineProperties it can be used as a “flags-aware” way of cloning an object: Normally when we clone an object, we use an assignment to copy properties, like this: …But that does not copy flags.

What are object property flags and descriptors?

These options, you can use to configure object properties, are called Object property flags and descriptors. Object property flags are attributes that each property in an object has. These flags are writable, enumerable and configurable. All these flags have boolean value.

How to use object defineproperty () method in JavaScript?

When you want to use this method you have to do three things. First, you need to create some object. This object can be empty if you want to use the Object.defineProperty () method to create property. If you want to use it to configure existing property that property has to already exist on that object.


1 Answers

Property descriptor objects does not exist unless explicitly requested. They are created ad-hoc when you call Object.getOwnPropertyDescriptor. So following code:

var foo = {bar:'foo'}
Object.getOwnPropertyDescriptor(foo, 'bar') === Object.getOwnPropertyDescriptor(foo, 'bar');

Always evaluate to false.

So as we see (both code and specification), property descriptor objects are not stored, but created on demand.

So where are writable, configurable, value, get, set... atributtes stored? Specification doesn't require them to be exposed to user code... Here is C++ definition for V8 PropertyDescriptor class - it seems like every property occupy one byte.

And if you want check if property is writable, configurable or similar, Firefox console allow you to do so (but only if property isn't writable or has getter/setter): Firefox DevTools

like image 75
Ginden Avatar answered Sep 17 '22 13:09

Ginden