最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Defining non-writable properties without Object.defineProperty? - Stack Overflow

programmeradmin6浏览0评论

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);

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);
Share Improve this question asked Jun 25, 2015 at 21:44 ZacqaryZacqary 2152 silver badges8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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)

No, there is no other method (except for Object.defineProperties and Object.create that allow you to provide multiple of these descriptors).

I'd really like to be able to keep that simple syntax

Notice that Object.defineProperty returns the passed object, so you can simplify your code to

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

and avoid introducing that extra obj variable.

With a naming convention, you can build it into your ObjectProcessor:

var prefix = 'readonly_';

function ObjectProcessor(obj) {
    Object.keys(obj).filter(function (name) {
        // find property names which match your convention
        return name.indexOf(prefix) === 0;
    }).forEach(function (name) {
        // make the new property readonly
        Object.defineProperty(obj, name.substring(prefix.length), {
            value: obj[name],
            writable: false
        });

        // remove the old, convention-named property
        delete obj[name];
    });

    // remaining ObjectProcessor logic...
}

This lets you handle writable and readonly properties:

ObjectProcessor({
    readonly_id: 1,
    name: 'Foo'
});
发布评论

评论列表(0)

  1. 暂无评论