Perfection kills

Exploring Javascript by example

Archives Posts

Named function expressions demystified

June 15th, 2009 by kangax

After a couple of months, I have finally finished an article on named function expressions. It’s meant to demystify some of the common misconceptions I’ve seen on the web, take an in-depth look at cross-browser quirks and explain how to safely “work around” them. Quite obviously, it also explains what named function expressions are good for and how to “take advantage” of them in your applications.

If you have suggestions or find any mistakes, please let me know. Hope you like it.

http://yura.thinkweb2.com/named-function-expressions/

Filed under Uncategorized having 10 Comments »

Archives Posts

Those tricky functions!

June 1st, 2009 by kangax

Are you still trying to write a regex matching decompiled function in Javascript? Perhaps, trying to detect a native method? Here’s a short but fun list of what you’ll be facing in various browsers. This is, of course, all very similar to userAgent adventures; Non-standard feature that is as reliable as my old 28K modem.

It’s probably better to abandon this idea altogether. Or at least not rely on it, and maybe use only for debugging purposes. Or, if you do like adventures after all, at least know what you’re dealing with:

Safari 1.3.x – 2.x

Does not match function expression syntax. Missing identifier.

Object; // (Internal Function)
document.getElementById; // [function]
(function foo(){ return a+b; }); // function(){ return a+b; }

Chrome 1.x

Missing identifier.

window.focus; // function () { [native code] }

Blackberry

Unique representation of function body. Source code is not available for anything but “small” functions

window.XMLHttpRequest;
// function XMLHttpRequest() { [native code for XMLHttpRequest.XMLHttpRequest, arity=1] }
 
function foo(){ /* ~10 lines of code */ };
// function foo() { \\Source code is not available. [1035593284ED8CD9D0734E9B14EF4F3FF6BE9686] }

Opera 10a

Missing identifier.

Array.prototype.push; // function () { [native code] }

Opera 10a Turbo Edition

Unique representation of function body.

document.getElementById; // function getElementById() { /* source code not available */ }

Caja

Unique representation of function body; Augmented identifier.

function f(){}; // function f() { [cajoled code] }
var f = function(){}; // function f$_var() { [cajoled code] }

Internet Explorer

Unique representation of function.

( /* foo bar */ (((function f(){}) /* baz qux */ ))); // ( /* foo bar */ (((function f(){}) /* baz qux */ )))

Rhino

Unique representation of function body.

({}).hasOwnProperty; // function hasOwnProperty() { [native code for Object.hasOwnProperty, arity=1] }

Do you have any other examples of strange-looking functions? Please, share. Perhaps, PPK will tell us how other mobile browsers handle function decompilation.

Edit (10/16/09):

  • Added Internet Explorer and its “unique” function representation. Thanks Andrea Giammarchi.
  • Added Rhino representation, which appears to be similar to that of Blackberry.
  • Updated Safari versions to include 1.3.x (which exhibits same behavior as 2.x)
Filed under don'ts having 2 Comments »