I have error when try to define property with accessors. Here is my code
var person = {};
Object.defineProperty(person, 'birthYear', {
value: 1997,
writable: true,
enumerable: true,
configurable: false,
set: function (value) {
this.birthYear = value;
},
get: function() {
return 'Birth year - ' + this.birthYear;
}
});
console.log(person.birthYear);
The error text:
Invalid property descriptor. Cannot both specify accessors and a value or writable attribute.
If i can't define set and get methods like this how i can do it?
valuebut you don't want abirthYeargetter that retrievesthis.birthYearbecause that would create infinite recursion. You need to store the value elsewhere, like on a different property name or symbol, or in a variable. - user1106925Object.create. - user1106925Object.prototype, and the object in the question doesn't have any other prototypes? - Bergi