JavaScript this 关键字的理解

  • this 关键字,如果前端面试有笔试,那么关于这个考点十有八九会有与其相关的题目的,说真的在实际的项目中 开发们也整不出那么重复的变量名吧。
  • 理解一段时间后,我发现自己就开始慢慢的忘记,还是好好梳理一下吧。

this当前对象的一个引用,始终指向调用它的对象

JavaScript 中 this 随着执行环境的改变而改变。

规则

  • 方法中,this 表示该方法所属的对象
  • 如果单独使用,this 表示全局对象。
  • 函数中,this 表示全局对象。
  • 构造函数中的this指定的是new关键字创建出来的对象
  • 函数中,在严格模式下,this 是未定义的(undefined)
  • 事件中,this 表示接收事件的元素
  • 类似== call() 和 apply() 方法可以将 this 引用到任何对象==。

什么是函数?

  • 函数就是没有和其它的类显示的绑定在一起的

什么是方法?

  • 方法就是显示的和其它的类绑定在一起的

函数与方法的区别

  • 函数可以直接调用, 但是方法不能直接调用, 只能通过对象来调用
  • 函数内部的this输出的是window, 方法内部的this输出的是当前调用的那个对象
方法中
var name = 'monkey"';
var person = {
  name: 'John',
  getName : function() {
    return this.name;
  }
};
person.getName();
// John
单独使用
function person() {
  return this;
}
person();
// window
构造函数

构造函数中的this指定的是new关键字创建出来的对象

function Person (name) {
  this.name =  name;
  this.getName = function() {
    return this.name;
  }
}
var obj = new Person('sunny');
obj.getName(); // sunny
巩固练习

对象方法 + 函数调用


var name = 'monkey';
var obj = {
    name : 'John',
    getName : function(){
        console.log(this.name);
    }
};
var a = obj.getName(); 
// John
var getName = obj.getName;
getName();
// monkey
var name = 'monkey';
var obj = {
    name : 'John',
    getName : function(){
        console.log(this.name);
        function other() {
        	console.log(this.name)
        }
        other();
    }
};
obj.getName();
// John
// monkey
// other 为函数调用模式,this指向的window

函数调用

var length = 99;
function getLen(){
   console.log(this.length);
}
var obj = {
   name : 5,
   method : function(getLen) {
       getLen();
       arguments[0]();
   }
};
obj.method(getLen, 'monkey', 'zoe', 'sunny', 'tony', 'eric');

贴上参考图示,因为现在我还并没有理解,哈哈哈
在这里插入图片描述

其他题目摘录(过了一遍,后面慢慢品味,哈哈哈)

var o = {
    a: 10,
    b: {
        a: 12,
        fn: function(){
            console.log(this.a); // 输出结果是 12
            console.log(this); // 输出结果是 b 对象
        }
    }
}
//调用
o.b.fn(); 
var o = {
    a: 10,
    b:  {
        fn: function(){
            console.log(this.a); // undefined
            console.log(this);   // b对象
        }
    }
}
//调用
o.b.fn(); 
var o = {
    a: 10,
    b: {
        a: 12,
        fn: function(){
            console.log(this.a); //undefined 若在对象o外定义a,则输出的就是其在外定义的值(全局变量)
            console.log(this);   // window
        }
    }
}
var j = o.b.fn; //只是将b对象下的方法赋值给j,并没有调用
j(); //调用,此时绑定的对象是window,并非b对象直接调用
var point = { 
    x : 0, 
    y : 0, 
    moveTo : function(x, y) { 
        this.x = this.x + x; 
        this.y = this.y + y;
        console.log(this.x); // 1
        console.log(this.y); // 1
    } 
}; 
point.moveTo(1, 1)//this 绑定到当前对象,即 point 对象
function someFun(x) { 
    this.x = x; 
} 
someFun(5); //函数被调用时,this绑定的是全局对象 window,相当于直接声明了一个全局变量x,并赋值为5
console.log(x); // x 已经成为一个值为 5 的全局隐式变量
var point = { 
    x : 0, 
    y : 0, 
    moveTo : function(x, y) { 
       // 内部函数
       var moveX = function(x) { 
           this.x = x;
       }; 
       // 内部函数
       var moveY = function(y) { 
           this.y = y;
       };
       moveX(x); // 这里是全局调用
       moveY(y); 
    }
}; 
point.moveTo(1, 1); 
console.log(point.x); // 0
console.log(point.y); // 0
var point = { 
	    x : 0, 
	    y : 0, 
	    moveTo : function(x, y) { 
	
	        var that = this; //内部变量替换
	
	        // 内部函数
	        var moveX = function(x) { 
	            that.x = x; 
	           // this.x = x;
	        }; 
	        // 内部函数
	        var moveY = function(y) { 
	            that.y = y;
	           // this.y = y;
	        } 
	        moveX(x); //这里依然是全局调用,但是在给变量赋值时,不再是this指向,而是that指向,而that指向的对象是 point。
	        moveY(y); 
	    } 
    }; 
    point.moveTo(1, 1); 
    console.log(point.x); // 1
    console.log(point.y); // 1
    console.log(x) // 报错 x is not defined
    console.log(y) //
function Point(x, y){ 
       console.log(this); // point对象
       this.x = x; 
       this.y = y; 
       this.moveTo = function(x,y){
          this.x = x;
          this.y = y;
          console.log(this.x);//1 10
          console.log(this.y);//1 10
       }
    }
    var p1 =  new Point(0,0); //注意这种形式方法的调用及apply、call的使用

    var p2 = {
        x:0,
        y:0
    }
    p1.moveTo(1,1); 
    p1.moveTo.apply(p2,[10,10]);

    console.log(x);// x is not defined
    console.log(y);// 
var a = 10;
var foo = {
    a: 20,
    fn: (function(){
        console.log(this); // window
        console.log(this.a); // 10
    })()
}
var a = 10;
var oTimer1 = setInterval(function(){
    var a = 20;
    console.log(this.a); // 10
    clearInterval(oTimer1);
},100);
(function(){
    eval("console.log(this)"); // Window
})();
function Foo(){
    this.bar = function(){
        eval("console.log(this)"); // Foo
    }
}
var foo = new Foo();
foo.bar();
var a = 10;
var foo = {
	a: 20,
	fn: function(){
	    console.log(this.a);
	}
};
var bar ={
    a: 30
}
foo.fn.apply();//10(若参数为空,默认指向全局对象)
foo.fn.apply(foo);//20
foo.fn();//20
foo.fn.apply(bar);//30

参考地址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

monkey01127

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值