this的指向
2019-04-25 本文已影响0人
爱笑的书生
this永远指向最后调用他的那个对象。
class point {
constructor(x=1,y=2){
this.x = x;
this.y = y;
}
say (){
console.log(this)
}
}
let pi = new pp(4);
pi.say()
输出=> { x: 4 ,y:2 }
构造函数本身是无法访问自身函数的,只有实例化才可以
var x=11;
var obj={
x:22,
say:()=>{
console.log(this.x);
}
}
obj.say();//输出的值为11
求数组的最大值
let arr = [1,2,3,1];
let maxValue = Math.max.apply(null,arr)