Getting and Setting Property Descriptors in JavaScript (Part 1)
In this post, I am going to tell you how you can get the property descriptor for a specific property in a JavaScript object. As I said in my previous post, property descriptors can be obtained with the help of two functions on the Object
constructor, viz. Object.getOwnPropertyDescriptor
and Object.getOwnPropertyDescriptors
functions.
First let's set the stage by creating a simple object with two properties:
let ali = {
age: 19,
get name() {
if (typeof this._name === "undefined") {
this._name = "Ali";
}
return this._name;
},
set name(name) {
this._name = name;
},
};
Here, age
is a data property and name
is an accessor property. Let's get the descriptor for the first property:
Object.getOwnPropertyDescriptor(ali, "age");
This gives us the following result:
{
value: 19,
writable: true,
enumerable: true,
configurable: true,
}
For the name
property, we get the following descriptor:
{
get: function () {
if (typeof this._name === "undefined") {
this._name = "Ali";
}
return this._name;
},
set: function (name) {
this._name = name;
},
enumerable: true,
configurable: true,
}
As you can see, attributes value
and writable
are only used for data properties. On the other hand, attributes get
and set
are obviously used only for accessor properties.
Now, what about the Object.getOwnPropertyDescriptors
function? This one gives you an object with all own property descriptors of the object. In other words, the resulting object maps property names to property descriptors. We can call this function like this:
Object.getOwnPropertyDescriptors(ali);
This gives us the following result:
{
age: {
value: 19,
writable: true,
enumerable: true,
configurable: true,
},
name: {
get: function () {
if (typeof this._name === "undefined") {
this._name = "Ali";
}
return this._name;
},
set: function (name) {
this._name = name;
},
enumerable: true,
configurable: true
},
}
As you can see, value
and writable
attributes are only used in data properties, and get
and set
attributes are only used for accessor properties.
In the next post, I am going to write about setting property descriptors.
Java script theory nice to see this..
I haven't started learning Java Script but I'm sure i can pick up one or two from you.
Hi @ghasemkiani, I randomly came across this post and this is probably my very comment on any of your posts. It's pleasure to know that authors like you are helping Steemians with their skills. I'm not a coder but I can see how much this will help the readers.
Keep up the good job and stay awesome.
Steem On!
Thank you for your kind words.
guideline me, How to lot of learn language JavaScript. example: some website share --
You may find w3schools.com JavaScript Tutorial useful. But there are a lot of other resources for learning JavaScript on the web.
Very well method to teach property descriptor for a specific property in a JavaScript object. Thanks a lot dear @ghasemkiani
#wow javascript next post dear @ghasemkiani thanx you so much .
some days ago start JavaScript. Javascript seems difficult to me. thanks for sharing good language.
Javascrip new next post thanx you so much
wellcome back @gjasemkiani, with a short break. your that project regarding "Property Descriptors in JavaScript" in this post is awsome and appreciable. best wishes.
i like this javascript dear @ghasemkiani .
thanks for sharing.