Understanding of the this keyword in js _ javascript skills

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.