I believe everyone is familiar with this keyword. The key of this in js is more confusing than that in oo language. Next, I will introduce the understanding of this keyword in js through this article, if you need this keyword, you can refer to the keyword this is provided in c ++ and java. It is difficult to start learning, but you only need to understand it, it is much more convenient to use. Next, I will explain the understanding of this keyword in js through this article.
This is a must-have question for many front-end interviews. Sometimes I see these questions on the Internet and try it myself. Well, it's really wrong! In actual development, you will also encounter this problem (although some class libraries will help us solve it), for example, when using some frameworks, such as: knockout, sometimes I don't understand why I don't use this directly, but I need to pass this as a parameter.
Next, let's talk about my understanding of it. It also serves as a note for future reference. If something is wrong, you are welcome to point out criticism.
1. Unlike C #, this must point to the current object.
Js's this point is uncertain, that is, it can be changed dynamically. Call/apply is a function used to change this point. this design can make the code more flexible and reusable.
2. this generally points to the function owner.
This is important! This is important! This is important!
This is also a common interview question, the following code: