call和apply 作用:改变this的指向
语法:
.call(对象,参数) 更改对应的this执行 将其对应的this执行 执行参数对象
参数1 (对象) :更改this指向的对象
参数2 (参数) :当前方法需要的参数
.apply(对象,[1,2,3])更改对应的this执行 将其对应的this执行 执行参数对象
将参数改成数组传递
function Student(name,age){
this.name=name
this.age=age
this.showInfo=function(){
console.log(this.name)
}
this.adc=function(a,b,c){
console.log(this.name+a+b+c)
}
}
var s1=new Student('wangyi',12)
var s2=new Student("王二",22)
s1.showInfo.call(s2)
s1.adc.call(s2,1,2,3)
s1.adc.apply(s2,[1,2,3])
Math对象 求一个数组的最大值,最小值?
var arr=[1,5,9,45,79,46,99];
var max=Math.max.apply(null,arr)
console.log(max)
var min=Math.min.apply(null,arr)
console.log(min)