var arr = [3, 10, 2, 100];
var max = Math.max.apply(Math, arr);
console.log(max); // 100
有大神能详细讲解一下吗,小白弄不懂
var arr = [3, 10, 2, 100];
var max = Math.max.apply(Math, arr);
console.log(max); // 100
有大神能详细讲解一下吗,小白弄不懂
Math.max
方法可以接受多个参数,现在你这是将参数列表组成了一个数组,Math.max
本身是不能接收一个数组的,现在需要一个能将数组转换为参数列表的一个工具,这个工具就是apply
var arr = [3, 10, 2, 100];
var max = Math.max.apply(Math, arr);
可以翻译为
Math.max(3, 10, 2, 100)
apply是怎么改变this的??这个得看源码了吧..就像
Array.prorotype.forEach(function(){
this....//你是想问这个this为什么会绑到调用数组的身上??这个好难。。
})
13 回答12.8k 阅读
7 回答1.9k 阅读
3 回答1.1k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
6 回答835 阅读✓ 已解决
6 回答1k 阅读
2 回答1.3k 阅读✓ 已解决
如何改变指针是标准定的,apply和call就是用来改变作用域的
call与apply的区别在于前者的参数是一个一个的,比如: