Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That code is doing three things:</p> <ol> <li>Using <em>object literal syntax</em> to create object instances</li> <li>Using <em>anonymous function expressions</em> to create functions and bind them to properties on the objects. (Functions are first-class objects in JavaScript, so you can keep references to them, pass the references around, etc.)</li> <li>Specifically, it's overriding two standard functions that all JavaScript objects inherit from the <code>Object</code> prototype.</li> </ol> <p>Let's break it down a bit.</p> <p>1) Object literal notation:</p> <pre><code>var obj = {propName: propValue}; </code></pre> <p>The <code>{</code> and <code>}</code> in this case denote an <em>object literal</em>. Within an object literal, you can write <code>propName: propValue</code> to assign <code>propValue</code> to the property with the name <code>propName</code> on the object. This is the same as:</p> <pre><code>var obj = {}; // Get an empty object obj.propName = propValue; // Add a property to it </code></pre> <p>You can do multiple properties separated with commas. So for instance:</p> <pre><code>var obj = { author: "Douglas Adams", title: "The Hitchhiker's Guide to the Galaxy", answer: 42 }; </code></pre> <p>That creates an object with three properties, two with string values and one with a number value.</p> <p>Note that the right-hand side are processed just like an assignment, and so can be anything that can appear on the right-hand side of an assignment statement:</p> <pre><code>var x = "bar"; var obj = { three: 1 + 2, fubar: "foo " + x }; </code></pre> <p>The property names can be put in quotes if you like:</p> <pre><code>var x = "bar"; var obj = { "three": 1 + 2, "fubar": "foo " + x }; </code></pre> <p>...which is handy for specifying properties that have the names of reserved tokens (like "if", or "return") or formerly-reserved tokens (like "class") where it would be a syntax error if they weren't in quotes.</p> <p>2) Now let's look at function expressions:</p> <pre><code>var f = function() { /* your code here */ }; </code></pre> <p>That's a function expression. It creates a new function and assigns a reference to it to the variable <code>f</code>. You can call it by calling <code>f()</code>.</p> <pre><code>var f = function(name) { alert("Hi " + name); }; f("Fred"); // alerts "Hi Fred" </code></pre> <p>1 + 2) So putting it together with object literal notation:</p> <pre><code>var obj = { foo: function(name) { alert("Hi " + name); } }; obj.foo("Fred"); // alerts "Hi Fred" </code></pre> <p>(I don't like anonymous functions, <a href="http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html" rel="nofollow">I prefer my functions to have names</a>, but that's another topic.)</p> <p>3) And finally: As maerics pointed out, the specific functions that are being used in that code are <code>toString</code> and <code>toLocaleString</code>, both of which are standard functions of JavaScript objects. That means that those will <em>override</em> the standard version and so return the given values whenever the standard function would have been called.</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