【JavaScript笔记】JavaScript中this的使用

JavaScript中this的调用方式

1)在方法中调用【注意 方法中声明函数,同时在函数中再调用this】
2)单独调用
3)在函数中调用

1)在方法中,调用this


// 结果如下
在person对象中调用方法内的this: [object Object]
对象方法中调用this --- person.fullName()结果: 前者 -- 后者

2)单独调用this


// 结果如下
单独调用this:[object Window]

3)在函数中,调用this


//结果如下:
1.  函数 myFunction--mySecond 嵌套函数中的this:[object Window]
2.  函数 myFunction--mySecond 嵌套函数中的this.i值:undefined
3.  函数中使用 this:[object Window]
4.  函数myFunction 中this.i值:undefined

在对象的方法中定义函数,同时定义的函数中调用this(重点)


//结果如下
1.  在person对象中调用方法内的this: [object Object]
2.  前者 -- 后者
3.  在对象 --方法 -- 函数中 this: [object Window]
4.  在对象 --方法 -- 函数中 _that: [object Object]
5.  在对象 --方法 -- 函数中 _that.lastName: 后者

箭头函数中的this

箭头函数其实没有自己的this,所以箭头函数内部的this就是外部的this


//结果如下:
1.  箭头函数里this:[object Object]
2.  箭头函数里 this.value: 100
3.  100


全部代码





菜鸟教程(runoob.com)






参考地址

菜鸟教程:https://www.runoob.com/js/js-this.html
js中this的四种使用场景:https://cloud.tencent.com/developer/news/201617
彻底理解js中this的指向,不必硬背--追梦子: https://www.cnblogs.com/pssp/p/5216085.html
JavaScript this:https://blog.csdn.net/fuziwang/article/details/84864020
JavaScript 的 this 原理:http://www.ruanyifeng.com/blog/2018/06/javascript-this.html

你可能感兴趣的:(【JavaScript笔记】JavaScript中this的使用)