Activate your free membership today | Log-in

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

Related Content:

Posted by Dion Almaer at 5:00 am
14 Comments

++++-
4.4 rating from 29 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.