前端复习--Math.min.apply(Math,arr)写法的理解
来源:互联网 发布:teradata数据库sql命令 编辑:程序博客网 时间:2024/06/16 03:44
Math.min.apply(Math,arr) 这种方法用于求一个数组中的最小值,这令我很困惑,我不知道为甚么apply的第一个参数为什么重新传入Math对象,
《JavaScript高级程序设计》上说:为了正确设置this的值。
这不是很好的解释,至少对于现阶段的我。
(1)顺藤摸瓜,我首先,研究了Math对象,从《Object-oriented javascript》上讲,
Math is a little different form the other built-in objects, because it cannot be used as a constructor to create objects. It is just a collection of functions and constants.
这貌似告诉我们,Math就是一个对象,min和max是它的方法,min和max接受的参数形式都是,Math.min(num1, num2, num3, ...)仅此而已。
(2)但是Math.min.apply(Math,arr) 这种奇怪的写法算什么呢?好吧,我们需要看看第二块,apply的用法。
call 和 apply的作用是什么?除了改变函数的this指向外,还有什么?---apply可以改变传递给函数参数的形式
从MDN上可以看到,这这属于apply的一种妙用
要理解的另一关键点:
apply的第一个参数,不可忽略!!!
if the method is a function in non-strict mode code, null
and undefined
will be replaced with the global object, and primitive values will be boxed
小结:对于普通函数fn,
function fn(arg1,arg2,arg3,...){
//....
}
fn(1,2,3)
fn.apply.(null,[1,2,3])
apply改变了参数的传入形式
- 前端复习--Math.min.apply(Math,arr)写法的理解
- Math.min.apply方法使用
- Math.min.apply数组排序
- Math.min.apply数组中取最大和最小
- js获取数组中最小的值的方法 Math.min.apply(),apply(),call()方法使用
- math.min() and math.abs()
- Math.round 的理解
- Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解
- Math.min() 为什么比 Math.max() 大?
- Math.round四舍五入的理解
- apply、call和Math.max
- Math
- math
- Math
- Math
- Math
- Math
- math
- addEventListener等事件监听的参数细谈
- Java读取文件内容并输出的最简单的方式之一(只需三行代码)
- 招投标领域深化改革的“长征”路途
- Java与数据库数据类型对应表
- Swift3.0语言教程获取字符
- 前端复习--Math.min.apply(Math,arr)写法的理解
- 欢迎使用CSDN-markdown编辑器
- 哈佛结构 和 冯·诺依曼结构
- 负载均衡,分布式,高可用,容错的区别
- React Native实战之flexbox布局
- 一篇很好的关于 java@SuppressWarings注解的文章,转载自http://www.cnblogs.com/fsjohnhuang/p/4040785.html
- 实例和其它对象的内建函数
- 与大家分享一下常用的Linux命令
- 从零开始学JDBC--1.11 事务机制以及案例分析