why calling apply on Math.max works and without it doens't
If you asked me to get the max of an array, I would just do:
var nums = [66,3,8,213,965,1,453];
Math.max.apply(Math, nums);
of course, I could also do: nums.sort(function(a, b){ return a -
b}.pop(nums.length);
but I have to be honest. I need to know WHY that works - using
.apply(Math,nums). If I just did this:
Math.max(nums);
that would not work.
by using apply, I pass in Math as this - and nums for the array. But I
want to know the intricacies of "why" that the first works and the latter
doesn't. What magic is happening?
There is something fundamental I am not wrapping my brains around. I have
read a bunch about "call and apply", but many times some cool tricks can
be had like the one above, and I feel I am missing something deeper here.
No comments:
Post a Comment