JavaScript中的this

JS中有两种模式,严格模式和非严格模式,我们看看这两种模式下this的情况

console.log(this); 
// Window
'use strict'
console.log(this); 
// Window
//两种模式下this的指向是相同的

函数内的this

  • 普通模式
function fn () {
  console.log(this); //window
  console.log(this === window); //true
}
fn()
  • 严格模式
'use strict'
function fn () {
  console.log(this); //undefined
  console.log(this === window); //false
}
fn()

不同模式下,函数内this的指向是不同的

call, apply, bind

  • js中有方法可以改变this的指向,我们分别调用试试,call和apply的不同在于参数的不同,apply接受一个类数组对象,call用,传入多个参数
  • call,apply
'use strict'
function thisWithCall () {
  console.log(this); //{name: 'harry'}
  console.log('name is', this.name); //name is harry
}

thisWithCall.call({name : 'harry'})

function thisWithApply () {
  console.log(this); //{name: 'potter'}
  console.log('name is', this.name); //name is potter
}

thisWithApply.apply({name : 'potter'})
  • bind创建一个新函数,此过程中改变this的指向,我们通过调用新函数使用改变的this
function thisWithBind () {
  console.log(this); //henry
  console.log('name is', this); //name is henry
}

let newBind = thisWithBind.bind('henry')
newBind()

箭头函数的this

  • 箭头函数没有this,也不能设置this,他们使用外层作用域中的this指向
const obj = {
  name : 'harry',
  getArrowName: () => {
    console.log(`name is ${this.name}`); //name is
    console.log(this); //window
  },
  getNormalName: function () {
    console.log(`name is ${this.name}`); //name is harry
    console.log(this); //{name: 'harry', getArrowName: ƒ, getNormalName: ƒ}
  }
}
obj.getArrowName()
obj.getNormalName()

箭头函数的this指向了全局作用域window,普通函数指向了obj的块级作用域

const count = {
  count : 0,
  addCount1 : function () {
    setTimeout(() => {
      console.log(this.count++); //0
      console.log(this); //{count: 1, addCount1: ƒ, addCount2: ƒ}
    })
  },
  addCount2: function () {
    setTimeout(function () {
      console.log(this.count++); //NaN
      console.log(this); //window
    })
  }
}
count.addCount1()
count.addCount2()

setTimeout的回调中,箭头函数的this指向了count块级作用域,普通函数指向了全局作用域

对象方法的this

var obj = {
  name : 'harry',
  getName : function() {
    return this
  }
}

console.log(obj.getName()); //{name: 'harry', getName: ƒ}
//this指向调用自己的对象

构造函数内的this

'use strict'
function Person (name, age) {
  this.name = name
  this.age = age
  this.getPerson = function () {
    return `我是${this.name},我${this.age}岁了`
  }
  this.returnThis = function () {
    return this
  }
}

var p = new Person('harry', 18)
console.log(p); //Person {name: 'harry', age: 18, getPerson: ƒ, returnThis: ƒ}
console.log(p.getPerson()); //我是harry,我18岁了
console.log(p.returnThis()); //Person {name: 'harry', age: 18, getPerson: ƒ, returnThis: ƒ}

构造函数内的this,指向其创建的实例

事件中的this

<button id="btn">点击</button>

//普通函数
var btn = document.querySelector('#btn')
btn.addEventListener('click', function () {
  console.log(this); //<button id="btn">点击</button>
})

//箭头函数
var btn = document.querySelector('#btn')
btn.addEventListener('click', () => {
  console.log(this); //window
})

按钮点击事件的this指向了按钮本身(普通函数),箭头函数中指向了全局window

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.概念 在JavaScript中,this 是指当前函数中正在执行的上下文环境,因为这门语言拥有四种不同的函数调...
    BluesCurry阅读 1,156评论 0 2
  • 在 Java 等面向对象的语言中,this 关键字的含义是明确且具体的,即指代当前对象。一般在编译期绑定。而 在 ...
    BubbleM阅读 258评论 0 0
  • 前言 本文为学习过程中的this小节,作为一名JavaScript自学未成才的编程人员,还没从“原型继承”中回过神...
    01_Jack阅读 756评论 0 3
  • 一.什么是this this是 JavaScript 语言的一个关键字。它是函数运行时,在函数体内部自动生成的一个...
    YINdevelop阅读 632评论 0 3
  • 前言 做为一个初学者,原型与闭包可以说是 JavaScirpt 中理解起来最难的部分了,当然,目前了解的也只是了解...
    小T果农阅读 352评论 0 0