简体   繁体   English

在没有Object.defineProperty的情况下定义不可写属性?

[英]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? 有没有办法以声明方式定义不可写/可枚举/可配置属性,而不是使用Object.defineProperty(),你定义一个getter或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);

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? 有没有办法以声明方式定义不可写/可枚举/可配置属性,而不是使用Object.defineProperty(),你定义一个getter或setter的方式?

No. 没有。

(there is Object.defineProperties , but I guess that's not what you are looking for) (有Object.defineProperties ,但我猜这不是你想要的)

No, there is no other method (except for Object.defineProperties and Object.create that allow you to provide multiple of these descriptors). 不,没有其他方法( Object.definePropertiesObject.create除外,它允许您提供多个这些描述符)。

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 请注意, Object.defineProperty返回传递的对象,因此您可以将代码简化为

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

and avoid introducing that extra obj variable. 并避免引入额外的obj变量。

With a naming convention, you can build it into your ObjectProcessor: 使用命名约定,您可以将其构建到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'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何列出用Object.defineProperty定义的属性 - How to List Properties Defined with Object.defineProperty Object.defineProperty:DOM元素属性的设置器 - Object.defineProperty: setters for dom elements properties 使用Object.defineProperty定义的属性的Javascript继承 - Javascript inheritance of properties defined with Object.defineProperty 设置员可以修改不可写/不可配置的属性吗? - Can setters modify non-writable/non-configurable properties? 如何在 Javascript 中修改不可配置、不可写的属性? - How to modify non-configurable, non-writable properties in Javascript? 为什么函数声明会覆盖全局对象的不可写属性? - Why does a function declaration override non-writable properties of the global object? 没有object.defineProperty的AngularJS装饰器 - AngularJS Decorator without object.defineProperty Object.defineProperty 不改变键顺序 - Object.defineProperty without changing keys order 在使用 object.defineProperty 方法之前在对象中定义属性 - defining a property in an object before using object.defineProperty method JavaScript:如何在不使用Object.defineProperty的情况下定义不可枚举的方法? - JavaScript: How to Define a Non-Enumerable Method without using Object.defineProperty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM