function in ur string

Jun 9, 2010

Old friend (and clearly damaged by a career of JavaScript programming) @jakedevine threw this down on twitter a few days ago.


   
var Z = "constructor";
    Z
[Z][Z]("alert('wtfjs!')")();
   
// alerts wtfjs!

Ok. So wtf exactly is happening here?


    Z
[Z]
   
// function String() { [native code] }

    Z
[Z][Z]
   
// function Function() { [native code] }

AHA! The constructor property of a string is String and the constructor property of String is Function. Makes sense.


    Z
[Z][Z]("console.log('new Function accepts string for eval as argument')")();
   
// new Function accepts string for eval as argument

Of course. One wonders, as usual, wtf the programmer was up to when he discovered this!

--- @brianleroux


void is a black hole

Jun 8, 2010

Void is a blackhole...


   
var void = function () {}
   
//=> SyntaxError: missing variable name

-- @rwaldron


instances and default values

Jun 2, 2010

When you create instances of String or Number, they take the default value ("" for strings and 0 for numbers). This is not the same for Object and Array.


   
var a = new Number;
    a
== 0 // true

   
var a = new String;
    a
== "" // true

   
var a = new Object;
    a
== {} // false

   
var a = new Array;
    a
== [] // false

This is even more confusing when using the JSON-style syntax to create objects and arrays.


   
var a = {};
    a
== {} // false

   
var a = [];
    a
== [] // false

-- @remi

(Never forget: ==== - @brianleroux)


syntax highlighting serverside with google prettyfy

May 1, 2010

This is much more awesome. I have to thank the guys at HowToNode.org for the idea in thier WheatJS static site blog engine. Essentially, it all boils down to running the rather awesome Google Code Prettyfy serverside.

The only remaining problem with the syntax highlighting is now nested <code> elements have to be escaped to display correctly in the final render.


   
// replace the raw code blocks with prettyfied html
    t
= t.replace(/<code>[^<]+<\/code>/g, function(code) {
       
return prettyfy(code.match(/<code>([\s\S]+)<\/code>/)[1]);
   
});    

Perhaps, in the future, a quick routine to fetch only the root code elements rather than blinding fetching all of them. Problem: more solved than before!


isNaN

May 1, 2010

So, null is not an object though sometimes null is a Number.


    isNaN
( null ); // false
   
null === NaN; // false
   
null == NaN; // false

isNaN converts null to number.


 
Number( null ); // 0

But WTF. :) - @oleg008


Fork me on GitHub