Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firefox and Chrome both support the <a href="https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FGuide/Constants" rel="nofollow noreferrer"><code>const</code></a> keyword. IE does not. So if you need constants and don't need to support IE, <code>const</code> isn't a bad choice. Keep in mind though, neither browser produces a runtime error when a value is assigned to a <code>const</code>; the values merely remains unchanged.</p> <p>Otherwise, you must use functions to define constants that cannot be modified:</p> <pre><code>function PI() { return 3.14159; } var area = radius*radius * PI(); </code></pre> <p>Of course, you could just write code that never modifies certain variables, and maybe establish a naming scheme for such variables such that you recognize that they will never need to be modified...</p> <pre><code>// note to self: never assign values to variables utilizing all-uppercase name var PI = 3.14159; </code></pre> <p>Another option for "simulating" constants would be to use the <a href="http://ejohn.org/blog/javascript-getters-and-setters/" rel="nofollow noreferrer">property definition functionality</a> available in some browsers to define read-only properties on an object. Of course, since the browsers supporting property definitions don't include IE, that doesn't really help... (note that IE<b>8</b> does support property definitions <a href="http://msdn.microsoft.com/en-us/library/dd229916.aspx" rel="nofollow noreferrer">after a fashion</a>... but not on JavaScript objects)</p> <p>Finally, in <em>very</em> contrived scenarios you might use function arguments as constants (perhaps this is what you were thinking of when you suggested closures?). While they behave as variables, they remain scoped to the function in which they are defined and cannot therefore affect the values held by variables with the same name outside of the function in which they are modified:</p> <pre><code>var blah = 3; var naw = "nope"; (function(blah, naw) { blah = 2; naw = "yeah"; alert(naw + blah); // "yeah2" })(blah, naw); alert(naw + blah); // "nope3" </code></pre> <p>Note that something similar to this is <a href="http://docs.jquery.com/Plugins/Authoring#Custom_Alias" rel="nofollow noreferrer">commonly used by jQuery plugins</a>, but for the opposite reason: jQuery code is usually written using the <code>$</code> shorthand to refer to the jQuery object, but the library is intended to continue working even if some other code redefines that symbol; by wrapping library and plugin code in anonymous functions with a <code>$</code> parameter and then passing in <code>jQuery</code> as an argument, the code is isolated from changes other libraries might make to the value of <code>$</code> later on.</p> <hr> <h3>See also: <a href="https://stackoverflow.com/questions/130396/are-there-constants-in-javascript">Are there constants in Javascript?</a></h3>
    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