this在函数中直接调用
function get(content){
console.log(content)
}
get("您好")
//另一种语法糖
get.call(window,"halou")
函数作为对象的方法被调用(谁调用我 我就指向谁)
var person={
name:"张三",
run:function(time){
console.log(`${this.name} 在跑步 最多 ${time}min不行了`);
}
}
person.run(20)
// 另一种语法糖
person.run.call(person,20)
箭头函数的this
在定义函数的时候绑定,不是在执行函数的时候绑定
箭头函数不能当作构造函数,没有自己的this,内部的this是最外层代码块的this,
var x=11
var obj ={
x:22,
say:()=>{
console.log(this.x);//箭头函数的this,指向的是最外层的代码块。是11
}
}
obj.say()