this was great. exactly what I have been looking for. I still have a few questions though...
function largest(){ return Math.max.apply( Math, arguments ); } <-- why is it necessary to run this function in the context of "Math". I don't get this...
function largest(){ return Math.max.apply( Math, arguments ); } <-- why is it necessary to run this function in the context of "Math". I don't get this...
It probably makes no difference what context the Math functions are executed in, since they are probably backed by native code implementations that don't rely on context, but one could imagine some JavaScript implementation taking a shortcut and doing something like:
Math.exp = function(p) {
return this.pow(this.E, p);
}
That function wouldn't work correctly unless it was executed in the Math context: assert(Math.exp(1), "Works");
assert(Math.exp.apply(Math, [1]), "Works");
var obj = {};
try {
Math.exp.apply(obj, [1]);
} catch(e) {
assert(false, "Doesn't work.");
}