this in Javascript
What is this?
In JavaScript, the this
keyword refers to an object.
Which object depends on how this
is being invoked (used or called).
The this
keyword refers to different objects depending on how it is used:
In an object method, this refers to the object. |
Alone, this refers to the global object. |
In a function, this refers to the global object. |
In a function, in strict mode, this is undefined . |
In an event, this refers to the element that received the event. |
Methods like call() , apply() , and bind() can refer this to any object. |
for the detail, see: https://www.w3schools.com/js/js_this.asp
also for bind method. see: https://www.javascripttutorial.net/javascript-bind/#:~:text=The%20bind%20%28%29%20method%20creates%20a%20new%20function%2C,This%20is%20known%20as%20function%20borrowing%20in%20JavaScript.
https://www.w3schools.com/js/js_function_bind.asp
当 this 和 arrow function在一起的时候,情况更加复杂了。但是记住这点:Arrow functions do not bind their own this
, instead, they inherit the one from the parent scope, which is called "lexical scoping".
https://www.codementor.io/@dariogarciamoya/understanding--this--in-javascript-du1084lyn
关于this in arrow function, 也可以看这个解释:https://stackoverflow.com/questions/63541194/meaning-of-enclosing-lexical-context-when-we-use-arrow-function-lexical-this
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律