Function.prototype.call()
call
方法使用一个指定的this
值和单独给出的一个或多个参数来调用一个函数。
1 | function Product(name, price) { |
Function.prototype.apply()
apply()
方法调用一个具有给定this
值的函数,以及作为一个数组(或类似数组对象)提供的参数。
1 | const numbers = [5, 6, 2, 3, 7]; |
备注:Math.max
max
是 Math
的静态方法,所以直接使用:Math.max()
,而不是创建的Math
实例的方法(Math
不是构造函数,通过typeof(Math)
可以看到,Math
是一个Object
,没有prototype
)。
语法
Math.max(value1[,value2,value3,…])
1 | Math.max(numbers) |
若不使用apply()
,运行结果为NaN,原因是相当于只提供了一个参数(numbers数组),max
方法给定的参数无法被转换为数字,则会返回NaN
。