How does .apply(Math, arrayName) work? (javascript)

后端 未结 4 1881
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 01:33

There is more than one stackoverflow question about how to find the min or max of an array of values in javascript. This is not that question.

I want to know why pas

4条回答
  •  终归单人心
    2021-01-16 01:55

    TL;DR - Math.min() and Math.max() don't actually use the this argument passed to them, it's just a hack to get them to accept an array of arguments, since the native API only specifies a list of variables. .apply() can accept arrays of arguments, which is why it is used.

    My guess is Math is passed in as the this argument simply for cleanliness' sake.

    Thanks to @Jonathan Lonowski and @Niet the Dark Absol.

提交回复
热议问题