一、function定义函数的this指向性不明确,随着使用环境的变化,发生改变。
箭头函数this指向性明确,即永远指向生产环境
<body>
<button id='bt1'>确定</button>
<script>
function Student(name,age){
this.name=name;
this.age=age;
}
let s1=new Student(125,125)
show()
function show(){
console.log(this)//this指向window
}
bt1.onclick=show//加个按钮
//封装到对像方法里
let obj={
name:"zhangsan",
show:show
}
obj.show()//obj
this的指向随着使用环境的不同而变化,例子中第一次指向window,第二次就指向obj对象
let show=()=>{
console.log(this)