Wednesday, November 19th, 2008
When generated JavaScript goes wild; JavaScript WTF in SharePoint
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."
-
-
function DeferCall() {
-
if (arguments.length == 0) {
-
return null;
-
}
-
var args = arguments;
-
var fn = null;
-
if (browseris.ie5up || browseris.nav6up) {
-
eval("if (typeof(" + args[0] + ")=='function') { fn=" + args[0] + "; }");
-
}
-
if (fn == null) { return null; }
-
if (args.length == 1) {
-
return fn();
-
} else if (args.length == 2) {
-
return fn(args[1]);
-
} else if (args.length == 3) {
-
return fn(args[1], args[2]);
-
} else if (args.length == 4) {
-
return fn(args[1], args[2], args[3]);
-
} else if (args.length == 5) {
-
return fn(args[1], args[2], args[3], args[4]);
-
} else if (args.length == 6) {
-
return fn(args[1], args[2], args[3], args[4], args[5]);
-
} else if (args.length == 7) {
-
return fn(args[1], args[2], args[3], args[4], args[5], args[6]);
-
} else if (args.length == 8) {
-
return fn(args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
-
} else if (args.length == 9) {
-
return fn(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
-
} else if (args.length == 10) {
-
return fn(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
-
} else {
-
var L_TooManyDefers_Text = "Too many arguments passed to DeferCall"
-
alert(L_TooManyDefers_Text);
-
}
-
return null;
-
}
-
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.
Old browsers don’t support function.apply().
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.
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
Hey, thats what they get payed the big bucks for.
loooool!
Douglas Crockford used to have a very similar snippet on his site for browsers that didn’t suppor function.apply()
Live and learn?
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)
}
Just implement Function.prototype.apply……
oh..my..god!
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.
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;
}
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));