ES6-箭头函数的this指向与普通函数的this指向

1.普通函数中的this

  1. 总是代表着它的直接调用者,如obj.fn,fn里的最外层this就是指向obj

  2. 默认情况下,没有直接调用者,this指向window

  3. 严格模式下(设置了’use strict’),this为undefined

  4. 当使用call,apply,bind(ES5新增)绑定的,this指向绑定对象

call 方法第一个参数是this的指向,后面传入的是一个参数列表。当第一个参数为null、undefined的时候,默认指向window。

apply 方法接受两个参数,第一个参数是this的指向,第二个参数是一个参数数组。当第一个参数为null、undefined的时候,默认指向window。

bind 方法和 call 方法很相似,第一个参数是this的指向,从第二个参数开始是接收的参数列表。区别在于bind方法返回值是函数以及bind接收的参数列表的使用。低版本浏览器没有该方法

示例:

let circle = {
	radius:10,
	outerDiameter:function(){
		let innerDiameter = function(){
			console.log(this); // window
			console.log(2*this.radius); // NaN
		};
		innerDiameter();
	}
}
circle.outerDiameter() 

对代码进行改造,输出正确的值

let circle = {
	radius: 10,
	outerDiameter: function () {
		let _this = this; //将this存放在一个变量中
		let innerDiameter = function () {
			console.log(2 * _this.radius); // 20
		};
		innerDiameter();
	}
}
circle.outerDiameter()

方法2

let circle = {
	radius: 10,
	outerDiameter: function () {
		let innerDiameter = function () {
			console.log(2 * this.radius); // 20
		};
        //使用bind绑定this指向
		innerDiameter = innerDiameter.bind(this);
		innerDiameter();
	}
}
circle.outerDiameter()

2.ES6箭头函数中的this

箭头函数本身不具备this,箭头函数的this指的是它在函数创建时外面作用域空间的this

this是静态的,this始终指向函数声明时所在作用域下的this的值

(1)默认指向定义它时,所处上下文的对象的this指向。即ES6箭头函数里this的指向就是上下文里对象的this指向,偶尔没有上下文对象,this就指向window

(2)即使是call,apply,bind等方法也不能改变箭头函数this的指向

示例:

(1) hello是全局函数,没有直接调用它的对象,也没有使用严格模式,this指向window

function hello() { 
   console.log(this);  // window 
}  
hello();

(2) hello是全局函数,没有直接调用它的对象,但指定了严格模式(‘use strict’),this指向undefined

function hello() { 
   'use strict';
   console.log(this);  // undefined
}  
hello();

(3)hello直接调用者是obj,第一个this指向obj,setTimeout里匿名函数没有直接调用者,this指向window

const obj = {
    num: 10,
   hello: function () {
    console.log(this);    // obj
    setTimeout(function () {
      console.log(this);    // window
    });
   }    
}
obj.hello();

(4)hello直接调用者是obj,第一个this指向obj,setTimeout箭头函数,this指向最近的函数的this指向,即也是obj

const obj = {
   num: 10,
   hello: function () {
    console.log(this);    // obj
    setTimeout(() => {
      console.log(this);    // obj
    });
   }    
}
obj.hello();

5)diameter是普通函数,里面的this指向直接调用它的对象obj。perimeter是箭头函数,this应该指向上下文函数this的指向,这里上下文没有函数对象,就默认为window,而window里面没有radius这个属性,就返回为NaN。

const obj = {
  radius: 10,  
  diameter() {    
      return this.radius * 2
  },  
  perimeter: () => 2 * Math.PI * this.radius
}
console.log(obj.diameter())    // 20
console.log(obj.perimeter())    // NaN
posted @ 2022-06-08 23:16  秋弦  阅读(35)  评论(0编辑  收藏  举报