By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,770 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,770 IT Pros & Developers. It's quick & easy.

apply() workaround

P: n/a
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Christopher Benson-Manica wrote:
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.