Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit:</strong> Since this answer was written, a new, better way using <code>Object.defineProperty</code> has been standardized in EcmaScript 5, with support in newer browsers. See <a href="https://stackoverflow.com/questions/366047/can-read-only-properties-be-implemented-in-pure-javascript/2964881#2964881">Aidamina's answer</a>. If you need to support "older" browsers, you could use one of the methods in this answer as a fallback.</p> <hr> <p>In Firefox, Opera 9.5+, and Safari 3+, Chrome and IE (tested with v11) you can define getter and setter properties. If you only define a getter, it effectively creates a read-only property. You can define them in an object literal or by calling a method on an object.</p> <pre><code>var myObject = { get readOnlyProperty() { return 42; } }; alert(myObject.readOnlyProperty); // 42 myObject.readOnlyProperty = 5; // Assignment is allowed, but doesn't do anything alert(myObject.readOnlyProperty); // 42 </code></pre> <p>If you already have an object, you can call <code>__defineGetter__</code> and <code>__defineSetter__</code>:</p> <pre><code>var myObject = {}; myObject.__defineGetter__("readOnlyProperty", function() { return 42; }); </code></pre> <p>Of course, this isn't really useful on the web because it doesn't work in Internet Explorer.</p> <p>You can read more about it from <a href="http://ejohn.org/blog/javascript-getters-and-setters/" rel="noreferrer">John Resig's blog</a> or the <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide/Creating_New_Objects/Defining_Getters_and_Setters" rel="noreferrer">Mozilla Developer Center</a>.</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