Search code examples
javascriptecmascript-5

Defining non-writable properties without Object.defineProperty?


So I know you can do this:

var obj = {};
Object.defineProperty(obj, 'staticProp', {
    value: 'I will never change',
    writable: 'false'
});

And I know you can do this:

var obj = {
    get gettableProp(){
        return 'gettable!'
    }
} 

Is there a way to define non-writable/enumerable/configurable properties declaratively instead of using Object.defineProperty(), the way you'd define a getter or setter?

The reason I ask is because I have a function that gets passed an object like this:

ObjectProcessor({
    // A bunch of properties
})

I'd really like to be able to keep that simple syntax for cases when I'd want to include non-writable or non-enumerable properties, rather than having to do

var obj = {}
Object.defineProperty(obj, 'staticProp', {
    value: 'I will never change',
    writable: 'false'
});
ObjectProcessor(obj);

Solution

  • Is there a way to define non-writable/enumerable/configurable properties declaratively instead of using Object.defineProperty(), the way you'd define a getter or setter?

    No.

    (there is Object.defineProperties, but I guess that's not what you are looking for)