Perfection Kills

by kangax

Exploring Javascript by example

← back 433 words

Those tricky functions

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] }


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

// 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 */ }


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 */ )))


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)

Did you like this? Donations are welcome

comments powered by Disqus