是否可以在 JavaScript 对象文字表示法中创建只读成员?

Kha*_*lla 0 javascript

我有以下 JavaScript 对象文字表示法对象

var Parameters= {
    modal_window:{
        backdrop:true,
        keyboard:true,
        show:true,
        remote:false,
        type:{
            normal:function(){
                this.footer.button.accept.type='btn btn-primary';
                this.header.type='modal-header';
            },
            success:function(){
                this.footer.button.accept.type='btn btn-success';
                this.header.type='modal-header alert alert-success';
            },
            info:function(){
                this.footer.button.accept.type='btn btn-info';
                this.header.type='modal-header alert alert-info';
            },
            error:function(){
                this.footer.button.accept.type='btn btn-danger';
                this.header.type='modal-header alert alert-error';
            },
            warning:function(){
                this.footer.button.accept.type='btn btn-warning';
                this.header.type='modal-header alert';
            }
        }
    },
    header:{
        title:undefined,
        type:this.window.type.normal.header
    },
    footer:{
        button:
        {
            accept:{
                title:'Accept',
                click:undefined,
                type:undefined
            },
            cancel:{
                title:'Cancel',
                click:undefined
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

是否可以使 header.type 和 footer.button.accept.type 只读变量,只能通过 window.type.normal、window.type.success 等更改?

澄清: 我想在这里做一些澄清。我的Parameters.header.type 应该是只读的并且应该有默认值。当用户选择例如Parameters.modal_window.type.normal时,必须更改Parameters.header.type。

pla*_*alx 5

不管大家怎么说,您可以在支持Object.defineProperty.

var obj = {};

Object.defineProperty(obj, 'someProp', {
    configurable: false,
    writable: false,
    value: 'initial value'
});

obj.someProp = 'some other value';

console.log(obj.someProp); //initial value
Run Code Online (Sandbox Code Playgroud)

编辑:

再次阅读您的问题后,我明白您的意思是真正的私有成员或私有变量。这可以通过使用闭包和自定义 getter/setter 来完成。

注意:为了示例,我简化了对象的结构。

var Parameters = (function () {
    var headerType = 'some value'; //private variable

    return {
        modal_window: {
            type: {
                normal: function () {
                    //custom logic
                    headerType = 'some new value'; //set private variable
                }
            }
        },
        header: {
            get type() { return headerType; } //define a getter only

            //for older browsers, you could just define a normal function
            //which you would have to access like Parameters.header.type()
            //type: function () { return headerType; }
        }
    };

})();

var header = Parameters.header;

console.log(header.type); //some value
header.type = 'some other val';
console.log(header.type); //some value
Parameters.modal_window.type.normal();
console.log(header.type); //some new value
Run Code Online (Sandbox Code Playgroud)

现在我们知道可以强制执行真正的隐私,但我不确定这是否真的值得。强制实施真正的隐私会使设计变得复杂并降低可测试性(取决于具体情况)。一种非常流行的方法是使用命名约定(例如_myPrivateVar. 这清楚地表明了这一意图,并告诉程序员他们应该将该成员视为私有成员。