Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Disclaimer:</strong> In terms of security constant / final variables are silly. In terms of increased performance because the js engine can optimise it more I doubt it makes a difference.</p> <p><a href="http://jsperf.com/freeze-vs-seal-vs-normal/2" rel="nofollow">Benchmark</a> shows no noticable increase in performance. This isn't actually useful apart from pedantry or pseudo security. </p> <p>The only use case is making part of your library frozen so that users don't shoot themself in the foot.</p> <p>With ES5 <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/freeze" rel="nofollow"><code>Object.freeze</code></a></p> <pre><code>window.MyModule = Object.freeze({ RED: "#FF0000", BLUE: "#0000FF", GREEN: "#00FF00", }); </code></pre> <p>You can (and it's <em>highly</em> recommended) use the <a href="https://github.com/kriskowal/es5-shim/" rel="nofollow">ES5-shim</a> to upgrade older/legacy browsers for compliance.</p> <p>You can also just use <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperties" rel="nofollow"><code>Object.defineProperties</code></a></p> <pre><code>window.MyModule = {}; Object.defineProperties(window.MyModule, { RED: { value: "#FF0000" }, BLUE: { value: "#0000FF" }, GREEN: { value: "#00FF00" } }); </code></pre> <p>There is a nice shortcut for the above using <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create" rel="nofollow"><code>Object.create</code></a></p> <pre><code>window.MyModule = Object.create(null, { RED: { value: "#FF0000" }, BLUE: { value: "#0000FF" }, GREEN: { value: "#00FF00" } }); </code></pre> <p>Be wary that sets the prototype to <code>null</code>, you may want to set it to <code>Object.prototype</code></p>
 

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