<rss version="2.0"><channel><description>wtfjs</description><title>wtfjs</title><generator>http://github.com/brianleroux/node-code-blog</generator><link>http://wtfjs.com/</link><item><title>min less max</title><description><p>This beauty is courtesy of <a href="https://github.com/TiTi">TiTi</a> ...lets look at some code.</p>

<pre><code>

    <span class="constant">Math</span>.<span class="identifier"><span class="keymethods">max</span></span>();
    <span class="regexp">//</span> -<span class="constant">Infinity</span>

    <span class="constant">Math</span>.<span class="identifier"><span class="keymethods">min</span></span>();
    <span class="regexp">//</span> <span class="constant">Infinity</span>

</code></pre>

<p>Ok, so, there <em>is</em> a good reason for this behaviour. It might even make sense if you happen to occasionally omit args from your min/max calls. ;)</p>

<p>You see, the min/max implementations need something to compare to and Infinity and -Infinity are the only safe values to use for that comparison. <a href="https://twitter.com/#!/kriskowal/status/85402842650783744">@kriskowell goes into more better detail here</a> and was quickly followed by <a href="https://twitter.com/#!/BrendanEich/status/85406752136368128">@brendaneich</a> whom not only <a href="http://brendaneich.com/2008/04/popularity/">wrote js in 10 days</a> but can rock out unicode Infinity symbols without looking them up ...I shit you not. </p>

<p>Of course, due to this behaviour js allows for this code humour:</p>

<p><code></p>

<pre><code>Math.min() &lt; Math.max();
// false
</code></pre>

<p></code></p>

<p>Oh JavaScript, I still love you.</p></description><link>http://wtfjs.com/2011/06/27/min-less-max</link><guid>http://wtfjs.com/2011/06/27/min-less-max</guid><pubDate>Mon Jun 27 2011 00:00:00 GMT+0000 (GMT)</pubDate></item><item><title>all your commas are belong to Array</title><description><p>This installment of wtfjs has to do with the Abstract Equality Comparison Algorithm (as most do), Array's constructor, and expressions.</p>

<p>Let's take the following example:</p>

<pre><code>
    <span class="keyword">new</span> <span class="built_in">Array</span>([],<span class="literal">null</span>,undefined,<span class="literal">null</span>) == <span class="string">",,,"</span>; // <span class="literal">true</span>
</code></pre>

<p>WTF? Why does this work?</p>

<p>Firstly, the == causes type coersion (pretty common).  From the ECMAScript Specification, 5th edition (final draft),
11.9.3 The Abstract Equality Comparison Algorithm:</p>

<blockquote>
  <p>The comparison <code>x == y</code>, where <code>x</code> and <code>y</code> are values produces true or false. Such a comparison is performed as follows:</p>
</blockquote>

<p>[snip]</p>

<blockquote>
  <ol>
  <li>If <code>Type(x)</code> is either String or Number and Type(y) is Object, return the result of the comparison <code>x == ToPrimitive(y)</code>.</li>
  <li>If <code>Type(x)</code> is Object and <code>Type(y)</code> is either String or Number, return the result of the comparison <code>ToPrimitive(x) == y</code>.</li>
  </ol>
</blockquote>

<p>So both uses of <code>Array</code> are run through <code>ToPrimitive</code>.  But what does <code>ToPrimitive</code> do, exactly?  Well, according to another
part of the Final final final final draft Standard ECMA-262 5th edition (the document seriously has this title...), 9.1. ToPrimitive:</p>

<p>[snip]</p>

<blockquote>
  <p>Object         Return a default value for the Object.  The default value of an object is retrieved by calling the <code>[[DefaultValue]]</code>
                 internal method of the object, passing the optional hint PreferredType.  The behavior of the <code>[[DefaultValue]]</code> internal
                 method is defined by this specification for all native ECMAScript objects in 8.12.8.</p>
</blockquote>

<p>So we're hinting to the [[DefaultValue]] method within Array with the type of <code>String</code>, so according (again) to the spec,
8.12.8 [[DefaultValue]] (hint):</p>

<blockquote>
  <ol>
  <li>Let toString be the results of calling the <code>[[Get]]</code> internal method of object O with argument "toString".</li>
  </ol>
</blockquote>

<p>Unless of course, <code>IsCallable(toString)</code> (i.e. the object has a <code>.toString</code> method on it's prototype).</p>

<blockquote>
  <ol>
  <li>If IsCallable(toString) is true, then,
     a. Let str be the results of calling the [[Call]] internal metho of toString with O as the this value and an empty argument list.</li>
  </ol>
</blockquote>

<p>And according to 15.4.4.2 Array.prototype.toString ():</p>

<blockquote>
  <p>When the toString method is called, the following steps are taken:</p>
  
  <ol>
  <li>Let array be the result of calling <code>ToObject</code> on the this value.</li>
  <li>Let func be the result of calling the <code>[[Get]]</code> internal method of array with argument "join".</li>
  </ol>
</blockquote>

<p>Oh, but we're not done yet!</p>

<p>Stay with me - we're type-coersing to a string, and <code>Array.prototype.toString</code> calls <code>Array.prototype.join</code> with no arguments, so we're
joining all the internal members of the array with the default separator is the single-character String "," (again, according to the spec).
When an Array calls join on itself, it's going from 1 .. len (all it's members) and calling <code>ToString</code> on these members and concatenating
them together.  Essentially doing this:</p>

<p><code></p>

<pre><code>Array.prototype.join = function (separator) {
    var result = "";
    if ("undefined" === typeof separator) {
        separator = ",";
    }
    for (var k = 0, len = this.length; k &lt; len; ++k &amp;&amp; result += separator) {
        var isToS = this[k] !== null &amp;&amp; this[k] !== undefined &amp;&amp; "function" === typeof this[k].toString
        result += isToS ? this[k].toString() : String(this[k]);
    }
    return result;
};
</code></pre>

<p></code></p>

<p>So in the end, we end up with weird stuff like this actually working, as <code>[]</code>, <code>null</code>, and <code>undefined</code> all result in "" when their
respective <code>ToPrimitive</code> methods ask for <code>[[DefaultValue]]</code> with String as the type hint.</p>

<p>Another similar WTF on the same topic:</p>

<pre><code>
    <span class="string">",,,"</span> == <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">4</span>); // <span class="literal">true</span>
</code></pre>

<p>This is similar, but not quite the same.  When you call Array's constructor, if there are multiple arguments, they're intepretted as being
members of the Array.  If you've only put 1 Integer (n) as the argument, an Array object is initiatilized with (n) <code>undefined</code> items.
Again, from the spec 15.4.2.2 new Array (len):</p>

<blockquote>
  <p>If the argument len is a <code>Number</code> and <code>ToUint32(len)</code> is equal to <code>len</code>, then the length property of the newly constructed object
  is set to <code>ToUint32(len)</code>.</p>
</blockquote>

<p>So essentially end up with</p>

<pre><code>
    [undefined,undefined,undefined,undefined].<span class="keyword">join</span>(),
</code></pre>

<p>Which yields something like:</p>

<pre><code>
    <span class="string">""</span> + String<span class="function">(<span class="title">undefined</span>)</span> + <span class="string">","</span> + String<span class="function">(<span class="title">undefined</span>)</span> + <span class="string">","</span> + String<span class="function">(<span class="title">undefined</span>)</span> + <span class="string">","</span> + String<span class="function">(<span class="title">undefined</span>)</span>
</code></pre>

<p>Which ends up being ",,," (which evaluates to <code>true</code>, as it matches).</p>

<p>Lastly, adding just one more level of WTF to this post, you can also accidentally (or intetionally?) add an expression within
Array's constructor function (and you can also omit new, as the spec also says: "When Array is called as a function rather than as a
constructor, it creates and initialises a new Array object.").</p>

<p>So we can finally end up with the weirdest rendition of this WTF as so:</p>

<pre><code>
    <span class="string">",,,"</span> == Array<span class="function">((<span class="title">null</span>,'<span class="title">cool</span>',<span class="title">false</span>,<span class="title">NaN</span>,4)</span>); // true
</code></pre>

<p>If this doesn't make you WTF, I'm not sure what will.</p>

<p><a href="http://twitter.com/danbeam">@danbeam</a></p></description><link>http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array</link><guid>http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array</guid><pubDate>Fri Feb 11 2011 00:00:00 GMT+0000 (GMT)</pubDate></item><item><title>convert to integer</title><description><p>The following will return an Ingeger with a default of 0 from any String or Number.</p>

<pre><code>
    function toInt(number) {
      return number &amp;&amp; + number | <span class="number">0</span> || <span class="number">0</span><span class="comment">;</span>
    }
    console<span class="preprocessor">.log</span>(toInt(<span class="string">"1"</span>))<span class="comment">;  // 1</span>
    console<span class="preprocessor">.log</span>(toInt(<span class="string">"1.2"</span>))<span class="comment">;  // 1</span>
    console<span class="preprocessor">.log</span>(toInt(<span class="string">"-1.2"</span>))<span class="comment">;  // -1</span>
    console<span class="preprocessor">.log</span>(toInt(<span class="number">1.2</span>))<span class="comment">;  // 1</span>
    console<span class="preprocessor">.log</span>(toInt(<span class="number">0</span>))<span class="comment">;  // 0</span>
    console<span class="preprocessor">.log</span>(toInt(<span class="string">"0"</span>))<span class="comment">;  // 0</span>
    console<span class="preprocessor">.log</span>(toInt(Number<span class="preprocessor">.NaN</span>))<span class="comment">;  // 0</span>
    console<span class="preprocessor">.log</span>(toInt(<span class="number">1</span>/<span class="number">0</span>))<span class="comment">;  // 0</span>
</code></pre>

<h2>Explanation</h2>

<pre><code>
    number &amp;&amp; <span class="keyword">x</span>
</code></pre>

<p>will return <pre><code><span class="keyword">x</span></code></pre> if <pre><code>number</code></pre> is truthy, and <pre><code>number</code></pre> otherwise.</p>

<pre><code>
    <span class="keyword">x</span> || <span class="number">0</span>
</code></pre>

<p>will return <pre><code><span class="keyword">x</span></code></pre> if <pre><code><span class="keyword">x</span></code></pre> is truthy, and 0 otherwise.</p>

<p><pre><code><span class="addition">+ number</span></code></pre> will convert number to a Number.</p>

<p><pre><code><span class="number">12.34</span> <span class="string">| 0</span></code></pre> will convert 12.34 to the integer <pre><code><span class="number">12</span></code></pre>. It will do it, as all binary operations can only be done on 32 bit integers in JavaScript.</p>

<p>-- <a href="http://twitter.com/Poetro">@Poetro</a></p></description><link>http://wtfjs.com/2010/12/06/convert-to-integer</link><guid>http://wtfjs.com/2010/12/06/convert-to-integer</guid><pubDate>Mon Dec 06 2010 00:00:00 GMT+0000 (GMT)</pubDate></item><item><title>i am myself but also not myself</title><description><p>Sometimes JavaScript has identity crisis:</p>

<pre><code>
var foo = [0];
console.log(foo == !foo);
console.log(foo == foo);
</code></pre>

<p>Is it time to have a "maybe" operator? :-P</p>

<p>--- <a href="http://twitter.com/diogobaeder">@diogobaeder</a></p></description><link>http://wtfjs.com/2010/11/15/i-am-myself-but-also-not-myself</link><guid>http://wtfjs.com/2010/11/15/i-am-myself-but-also-not-myself</guid><pubDate>Mon Nov 15 2010 00:00:00 GMT+0000 (GMT)</pubDate></item><item><title>false advertising</title><description><p>What do you think this constructor returns for <code>new Dude('Bob')</code>? Doug or Bob?</p>

<pre><code>
    function Dude(name){
        this.name = name;
        <span class="keyword">return</span> {name: <span class="string">'Doug'</span>};
    }
</code></pre>

<p>Answer:</p>

<pre><code>
    var bob = <span class="keyword">new</span> Dude(<span class="comment">'Bob');</span>
    // { name: <span class="comment">'Doug' }</span>
    bob instanceof Dude
    // <span class="literal">false</span>
</code></pre>

<p>Huh!? So you can just slip in anything? What about arrays?</p>

<pre><code>
    function Dude(name){
        this.name = name;
        <span class="keyword">return</span> [1,<span class="number"> 2</span>,<span class="number"> 3</span>];
    }
    new Dude(<span class="string">'Bob'</span>);
    // [1, 2, 3]
</code></pre>

<p>That can't be! What about...</p>

<pre><code>
    <span class="identifier">function</span> <span class="constant">Dude</span>(<span class="identifier"><span class="keymethods">name</span></span>){
        <span class="identifier">this</span>.<span class="identifier"><span class="keymethods">name</span></span> = <span class="identifier"><span class="keymethods">name</span></span>;
        <span class="identifier"><span class="keyword">return</span></span> <span class="number">3</span>;
    }
    <span class="identifier"><span class="keymethods">new</span></span> <span class="constant">Dude</span>(<span class="string">'Bob'</span>);
    <span class="regexp">//</span> { <span class="identifier"><span class="keymethods">name</span></span><span class="symbol">:</span> <span class="string">'Bob'</span> }
</code></pre>

<p>Wah? No way! So, if you try to return a primitive type from a constructor(number, string, date), it
will ignore the return value and return the originally initialized object, but otherwise, the returned value overrides.</p>

<p>--- <a href="http://twitter.com/airportyh">@airportyh</a></p></description><link>http://wtfjs.com/2010/11/10/false-advertising</link><guid>http://wtfjs.com/2010/11/10/false-advertising</guid><pubDate>Wed Nov 10 2010 00:00:00 GMT+0000 (GMT)</pubDate></item></channel></rss>
