javascript - Someone please explain the Function.apply.bind(Math.max, null) algorithm - Stack Overflow

suppose we have this code function largestOfFour(arr) {return arr.map(Function.apply.bind(Math.max, nul

suppose we have this code

function largestOfFour(arr) {
  return arr.map(Function.apply.bind(Math.max, null));
}

where arr is an array of arrays.

  1. first,why must i use apply()?

I understand that when using the method Math.max() to operate on an array i must add the apply() method also. So i'll have something like this Math.max.apply(null, arr) why? what does apply() do?

  1. In this code arr.map(Function.apply.bind(Math.max, null)) what does bind() really do?

Please give an explanation i can understand,i really appreciate this.

suppose we have this code

function largestOfFour(arr) {
  return arr.map(Function.apply.bind(Math.max, null));
}

where arr is an array of arrays.

  1. first,why must i use apply()?

I understand that when using the method Math.max() to operate on an array i must add the apply() method also. So i'll have something like this Math.max.apply(null, arr) why? what does apply() do?

  1. In this code arr.map(Function.apply.bind(Math.max, null)) what does bind() really do?

Please give an explanation i can understand,i really appreciate this.

Share Improve this question edited Apr 20, 2019 at 2:31 dance2die 37k39 gold badges136 silver badges198 bronze badges asked Sep 19, 2017 at 22:47 John AnisereJohn Anisere 5421 gold badge3 silver badges15 bronze badges 1
  • bind recreate the function with own properties, if you already used the event can be recreate ... etc ... – user8556290 Commented Sep 19, 2017 at 22:52
Add a ment  | 

3 Answers 3

Reset to default 7

Looking at the entire expression:

arr.map(Function.apply.bind(Math.max, null));

map expects its first argument to be a function, which is returned by:

Function.apply.bind(Math.max, null);

Function.apply is a shorter version of Function.prototype.apply.

Calling bind on it returns a special version of apply whose this is set to Math.max and when called, will have as it's first parameter (i.e. the value that would normally be used as this) set to null, since it's not going to be used.

So each element in arr will effectively be called using:

Math.max.apply(null, member);

The use of apply means the values in member are passed as parameters, as if:

Math.max(member[0],member[1] ... member[n]); 

So the expression returns the maximum value in each array. Those values are returned to map, which puts them into a new array.

var arr = [[1,2,3],[4,5,6]];
console.log(
  arr.map(Function.apply.bind(Math.max, null)) //[3, 6]
);

and is effectively the same as:

var arr = [[1, 2, 3],[4, 5, 6]];
console.log(
  arr.map(function(a) {return Math.max.apply(null, a)}) //[3, 6]
);

Though using recent features you might use destructing with rest parameter syntax:

var arr = [[1, 2, 3],[4, 5, 6]];
console.log(
  arr.map(a => Math.max(...a))   // [3, 6]
);

Simply put, .apply calls a function with the set of arguments(array-like) passed to it.

EG:

const add = (...args) => args.reduce((acc, next) => acc + next);

I can call the add function with any number of arguments using the .apply method like this.

add.apply(null, [4, 2, 6, 76, 9]) // => 97.

You call also use .call but instead of passing in array-like arguments, you simply pass in the values

add.call(null, 4, 2, 6, 76, 9) // => 97.

With .bind, the difference is that it creates a new function with call be called later.

const addFunc = add.bind(null, 4, 2, 6, 76, 9);
addFunc() // -> 97.

So, as it applies to the function we defined, it also applies to inbuild functions like Math.max, Math.min, etc.

Hope this helps!

The Function.apply.bind(Math.max, null) creates a function definition when invoked takes null as the first parameter by default and any provided parameters will e second. So as a callback to arr.map this function (due to bind statement) will be bound to Math.max however the Function.apply's first parameter will be null and second is going the be the sub array item of the main array (of which the items are to be passed as arguments to Math.max function).

This is an old trick and in ES6 terms arr.map(s => Math.max(...s)); would do the same job much more clearly.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744929057a4601620.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信