31 ms·
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 );
by jdbeast00 17y ago
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...
- jcl 17y agoI would guess that it's just good programming practice to feed a function the context in which it would normally be called -- which for the Math.max function is the Math namespace object. 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."); }
- jdbeast00 17y agothank you. i suppose this makes sense, i guess i don't understand objects / functions as well as I thought. I thought that Math was like a static thing from other languages, where you wouldn't ever create a new Math() and therefore the this operator wouldn't make sense...