apply() workaround

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

IE 5.0 apparently does not support the apply() method for function
objects - the following fails:

function foo() {
alert( this.length );
}
foo.apply( [] );

Is there a convenient way to add an apply() method to foo that would
allow this code to work as written?
 
D

Douglas Crockford

Christopher said:
IE 5.0 apparently does not support the apply() method for function
objects - the following fails:

function foo() {
alert( this.length );
}
foo.apply( [] );

Is there a convenient way to add an apply() method to foo that would
allow this code to work as written?

if (!isFunction(Function.apply)) {
Function.method('apply', function (o, a) {
var r, x = '____apply';
if (!isObject(o)) {
o = {};
}
o[x] = this;
switch ((a && a.length) || 0) {
case 0:
r = o[x]();
break;
case 1:
r = o[x](a[0]);
break;
case 2:
r = o[x](a[0], a[1]);
break;
case 3:
r = o[x](a[0], a[1], a[2]);
break;
case 4:
r = o[x](a[0], a[1], a[2], a[3]);
break;
case 5:
r = o[x](a[0], a[1], a[2], a[3], a[4]);
break;
case 6:
r = o[x](a[0], a[1], a[2], a[3], a[4], a[5]);
break;
default:
alert('Too many arguments to apply.');
}
delete o[x];
return r;
});
}


See http://www.crockford.com/javascript/remedial.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top