Wednesday, November 19th, 2008

When generated JavaScript goes wild; JavaScript WTF in SharePoint

Category: Fun

The JavaScript below was pointed out by Aaron Newton, and it comes from SharePoint. Jim Wilson said about it: “and we wonder why windows has 40 million lines of code.”

javascript

  1. function DeferCall() {
  2.     if (arguments.length == 0) {
  3.         return null;
  4.     }
  5.     var args = arguments;
  6.     var fn = null;
  7.     if (browseris.ie5up || browseris.nav6up) {
  8.         eval("if (typeof(" + args[0] + ")=='function') { fn=" + args[0] + "; }");
  9.     }
  10.     if (fn == null) { return null; }
  11.     if (args.length == 1) {
  12.      return fn();
  13.     } else if (args.length == 2) {
  14.         return fn(args[1]);
  15.     } else if (args.length == 3) {
  16.         return fn(args[1], args[2]);
  17.     } else if (args.length == 4) {
  18.         return fn(args[1], args[2], args[3]);
  19.     } else if (args.length == 5) {
  20.         return fn(args[1], args[2], args[3], args[4]);
  21.     } else if (args.length == 6) {
  22.         return fn(args[1], args[2], args[3], args[4], args[5]);
  23.     } else if (args.length == 7) {
  24.         return fn(args[1], args[2], args[3], args[4], args[5], args[6]);
  25.     } else if (args.length == 8) {
  26.         return fn(args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
  27.     } else if (args.length == 9) {
  28.         return fn(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  29.     } else if (args.length == 10) {
  30.         return fn(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
  31.     } else {
  32.         var L_TooManyDefers_Text = "Too many arguments passed to DeferCall"
  33.         alert(L_TooManyDefers_Text);
  34.     }
  35.     return null;
  36. }

Posted by Dion Almaer at 5:00 am
14 Comments

++++-
4.2 rating from 34 votes

14 Comments »

Comments feed TrackBack URI

That reminds me my coding style around 1995.

if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then if then..

At least, he could use a Switch/Case or something.

Comment by ccan — November 19, 2008

Old browsers don’t support function.apply().

Comment by Jordan1 — November 19, 2008

If you think this code is bad you ought to see how the database is constructed for SharePoint.

@Jordan1: the server-side code for SharePoint does browser detection and changes the code sent down to the client. SharePoint for the most part works in FF but renders differently (quite a bit in places) than IE.

Comment by MAVickers — November 19, 2008

I might be missing something, but this looks more like handwritten than generated code to me.

Microsoft has a long history of generating awful javascript, good thing they turned to jQuery for their MVC framework

Comment by kristianj — November 19, 2008

Hey, thats what they get payed the big bucks for.

Comment by TNO — November 19, 2008

loooool!

Comment by darkoromanov — November 19, 2008

Douglas Crockford used to have a very similar snippet on his site for browsers that didn’t suppor function.apply()

Comment by quiiver — November 19, 2008

Live and learn?

Comment by TNO — November 19, 2008

Would something like this work? (when Function.apply isn’t there)

function DeferCall(){
var args = arguments, str = ‘(function(g){return g[0](‘;
for(var i = 1; i <= args.length; i++){
str += ‘g[‘+i+’]’+(i==args.length?”:’, ‘)
}
return eval(str+”)})”)(args)
}

Comment by antimatter15 — November 19, 2008

Just implement Function.prototype.apply……

Comment by TNO — November 19, 2008

oh..my..god!

Comment by jaysmith — November 19, 2008

not only are some browsers missing function.apply(), but some functions in otherwise modern browsers don’t get it either… like for instance the “functions” that a SWF registers as ExternalInterface callbacks.

Comment by shadedecho — November 20, 2008

This function can be re-written as follows:
function DeferCall(fn) {
if(typeof(fn)==’function’)
{
if (arguments && arguments.length > 1)
{
fn.apply(null, Array.prototype.slice.call(arguments,2));
}
else
{
fn.call();
}
}
return null;
}

Comment by ASwett — December 22, 2008

Correction on my post, the slice.call command used to create the arguments array should start at the index 1, rather than 2.
fn.apply(null, Array.prototype.slice.call(arguments,1));

Comment by ASwett — December 23, 2008

Leave a comment

You must be logged in to post a comment.