Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Despite what everyone says, you can create read-only properties in modern browsers that supports <code>Object.defineProperty</code>.</p> <pre><code>var obj = {}; Object.defineProperty(obj, 'someProp', { configurable: false, writable: false, value: 'initial value' }); obj.someProp = 'some other value'; console.log(obj.someProp); //initial value </code></pre> <p><strong>EDIT:</strong></p> <p>After reading your question again, I understand that you meant true private members or private variables. That can be accomplished by making use of closures and custom getters/setters. </p> <p><em>Note: I simplified your object's structure for the sake of the example.</em></p> <pre><code>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 </code></pre> <p>Now that we know it is possible to enforce true privacy, I am not sure it's really worth it. Enforcing true privacy complicates the design and reduces testability (depending on the case). An approach that is far popular as well is to simply identify private members using a naming convention such as <code>_myPrivateVar</code>. This clearly indicates the itention and tells the programmers that they should treat that member like a private one.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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