Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" rel="nofollow">property</a> and set it as non-writable. Your constructor would have to replace the values with the properties. If the variable that the property is returning is captured in a closure and not exposed to anything else, it will be as good as read-only. If it is not changed, you don't even need a closure, just use the <code>value</code> configuration option.</p> <p>EDIT: As per your demand,</p> <pre><code>var Properties = function(obj) { var makePropRecursive = function(prop) { var old_prop = obj[prop]; delete obj[prop]; var prop_obj = {}; for (var attr in old_prop) { if (old_prop.hasOwnProperty(attr)) { Object.defineProperty(prop_obj, attr, { value: old_prop[attr], writable: false, enumerable: true }); } } Object.defineProperty(obj, prop, { value: prop_obj, writable: false, enumerable: true }); }; makePropRecursive('header'); makePropRecursive('footer'); return obj; }; var props = new Properties({ 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:"Whatever", type:"Type" }, footer:{ button: { accept:{ title:'Accept', click:undefined, type:undefined }, cancel:{ title:'Cancel', click:undefined } } } }); console.log(props.header); props.header = 17; props.header.type = 18; props.header.title = 19; console.log(props.header); </code></pre> <p><code>props.header</code> is unchanged: output shows</p> <pre><code>Object {title: "Whatever", type: "Type"} Object {title: "Whatever", type: "Type"} </code></pre> <p>It's 3am and the recursive function isn't, so you can only "fix" one level of one object; also, it would be better if the values were copied onto <code>this</code> rather than returning <code>obj</code>; but it should not be too hard to polish it up.</p> <p>If you need to have the values changeable, you can set up a private copy of the whole object inside the constructor, then make a getter (<code>get: function(name) { return stuff.from.the.original.object }</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload