Error: Uncaught TypeError property description must be an object : h
Getting an above error while using Object.defineProperty() method to add setters and getters to an already existing object to modify values of property in a object.
const jump = {
height: "10:00mtr",
time: "10seconds",
}
//adding a setters and getters to height.
Object.defineProperties(jump, "height2", {
get: function () {
return this.height;
},
set: function (newHeight) {
return this.height = newHeight;
},
});
//adding a setters and getters to time.
Object.defineProperties(jump, "time2", {
get: function () {
return this.time;
},
set: function (newTime) {
return this.time = newTime;
}
});
This particular syntax you use is reserved to Object.defineProperty
rather than Object.defineProperties
. Your only mistake is then using Object.defineProperties
where you was supposed to use Object.defineProperty
.
Even in your question title you ask about Object.defineProperty
but still you use Object.defineProperties
in the code below.
Corrected code:
Object.defineProperty(jump, "height2", {
get: function () {
return this.height;
},
set: function (newHeight) {
return this.height = newHeight;
},
});
On the other hand, Object.defineProperties
is used to define multiple propeties at once and should be used like this:
Object.defineProperties(jump,
{
"height2":
{
get: function () {
return this.height;
},
set: function (newHeight) {
return this.height = newHeight;
}
},
"anotherProperty:
{
...
}
});