关于this的总结
<script>
console.log(this)
function abc() {
console.log(this)
}
abc()
document.addEventListener("click",clickHander)
function clickHander(e){
console.log(this)
}
var obj = {
a:1,
b:function(){
console.log(this)
}
}
obj.b()
var obj2 = Object.create(obj)
obj2.name="zhang san"
obj2.b()
function Person(){
}
Person.prototype={
name:"li si",
age:"18",
c:function(){
console.log(this===one)
}
}
one = new Person()
one.c()
class Ball{
constructor(){
}
paly(){
console.log(this===ball)
}
}
ball = new Ball()
ball.paly()
function bindHander(a,b){
this.a=a;
this.b=b;
}
bindHander(3,5)
console.log(this.a,this.b)
var objs = {};
bindHander.call(objs,3,5)
console.log(objs)
bindHander.apply(objs,[3,5])
console.log(objs)
bindHander.bind(objs)(3,5);
console.log(objs)
var name = "windows"
var object = {
name:"object",
getName:function(){
return function(){
return this.name
}
}
}
console.log(object.getName()())
var name = "windows"
var object = {
name:"object",
getName:function(){
var _this=this
return function(){
return _this.name
}
}
}
console.log(object.getName()())
var name = "windows"
var object = {
name:"object",
getName:function(){
return this.name
}
}
console.log(object.getName())
console.log((object.getName)())
</script>