Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  124] [ 1]  / answers: 1 / hits: 15835  / 13 Years ago, wed, august 3, 2011, 12:00:00

I'm trying to define an object and create an accessor property for it.



HTML:



<input type='hidden' id='crudMode' value='Create' />


JavaScript:



crudMode = {
create: Create,
read: Read,
update: Update,
delete: Delete,
current: function () { return $('#crudMode').val(); }
}

Object.defineProperty(crudMode, 'mode', {
get: function(){
return this.current();
},
set: function(value){
$('#crudMode').val(value);
}
});


But when I use it, it throws the mentioned error in the question title:



console.log(crudMode.mode);


Throws:




TypeError: can't redefine non-configurable property 'mode'




What's wrong here?


More From » properties

 Answers
43

MDC documentation says that, as well as 'get' and 'set', you need a flag 'configurable' set to true when calling Object.defineProperty.



https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty


[#90848] Monday, August 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mariselas

Total Points: 711
Total Questions: 117
Total Answers: 110

Location: Burkina Faso
Member since Thu, Dec 23, 2021
2 Years ago
;