Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because <code>50</code> by itself is a Number <em>value</em>, whereas you are returning a Number <em>object</em>.</p> <pre><code>alert(typeof 50); // number alert(typeof (50).abs()); // object </code></pre> <p><a href="http://jsfiddle.net/Pkkaq/" rel="noreferrer">http://jsfiddle.net/Pkkaq/</a></p> <p>Section 4.3.21 of the <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" rel="noreferrer">ECMAScript reference</a>:</p> <blockquote> <p>A Number object is created by using the Number constructor in a new expression, supplying a Number value as an argument. The resulting object has an internal property whose value is the Number value. A Number object can be coerced to a Number value by calling the Number constructor as a function.</p> </blockquote> <p>In other words, a Number <em>value</em> cannot be strictly equal to a Number <em>object</em>.</p> <pre><code>typeof 50 === typeof new Number(50) //--&gt; false; number != object </code></pre> <p>The reason why <code>(-50).abs()</code> works as expected is because it is being multiplied by <code>-1</code>. When a Number <em>object</em> is multiplied by a Number <em>value</em> it becomes a Number <em>value</em>. In this case, if the parameter is positive, the object is simply returned untouched, causing an <em>object</em> to be returned.</p> <p>Here is a fix for your method, according to the quoted reference above:</p> <pre><code>Number.prototype.abs = function () { return Number(this &gt;= 0 ? this : this * -1); }; </code></pre>
    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. 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