如何深入理解JavaScript中this的指向机制及其修改方法?

更新于
2026-09-27 23:21:18
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1012个文字,预计阅读时间需要5分钟。

如何深入理解JavaScript中this的指向机制及其修改方法?

目录 + this 方法和对象中 + 隐藏的 this + 严格模式 + 可以改变 this 指向 + this 老规矩先看代码:

方法中 + function test() { console.log(this); } + 对象中 + Person={ name: 张三, eat: function() { console.log(this); } } + 在方法中

目录
  • this
    • 方法中
    • 对象中
    • 隐藏的this
    • 严格模式
    • 可以改变this指向

this

老规矩先看代码:

方法中

function test(){ console.log(this); }

对象中

Person={ name:"张三", eat:function(){ console.log(this) } }

在方法中,this表示该方法所属的对象。因为第一个是window上的方法,所以打印了window,而eat方法是Person方法,所以打印除了对象Person。

所以可以看出单独在控制台使用this, 表示全局对象。

阅读全文
标签:指向以及

本文共计1012个文字,预计阅读时间需要5分钟。

如何深入理解JavaScript中this的指向机制及其修改方法?

目录 + this 方法和对象中 + 隐藏的 this + 严格模式 + 可以改变 this 指向 + this 老规矩先看代码:

方法中 + function test() { console.log(this); } + 对象中 + Person={ name: 张三, eat: function() { console.log(this); } } + 在方法中

目录
  • this
    • 方法中
    • 对象中
    • 隐藏的this
    • 严格模式
    • 可以改变this指向

this

老规矩先看代码:

方法中

function test(){ console.log(this); }

对象中

Person={ name:"张三", eat:function(){ console.log(this) } }

在方法中,this表示该方法所属的对象。因为第一个是window上的方法,所以打印了window,而eat方法是Person方法,所以打印除了对象Person。

所以可以看出单独在控制台使用this, 表示全局对象。

阅读全文
标签:指向以及