Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in accessor property: can't redefine non-configurable property 'status'

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?

like image 794
Saeed Neamati Avatar asked Aug 03 '11 09:08

Saeed Neamati


1 Answers

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

like image 128
Matthew Wilson Avatar answered Oct 01 '22 17:10

Matthew Wilson