JS Common var that = the use of this

Source: Internet
Author: User

This is a keyword in the JavaScript language. It represents an internal object that is automatically generated when the function is run, this represents the current object and can only be used inside the function

var that=this is a copy of the current this object into that variable

$ (' #conten'). Click (function () {    //This is the #conten    var that is clicked this;    $ ('. Conten'). each (function () {    //-This is the current object    in the. Conten loop  //that is still just clicked on #conten    });

As you can see, the This object is changed at any time in the program, and after Var that=this, that is still pointing to the then-current this, so that no previous object can be found

jquery jquery has a particularly typical example of what it means to use _this.
$ ("#btn"). Click (function () {    varthis;   here This and _this both represent the "#btn" of this object    $ (". TR"). each (function () {           this; // here This represents each of the ". Tr" Objects          that are traversed _this; // still stands for "#btn" Object     })})

This is the case in a code fragment where this may represent a different object, and the coder wants _this to represent the original object

JS can be nested multiple layers of code, perhaps the following can also be embedded in a method, reference this will become the sub-method control of the object, if the need for superior image,
In the absence of parameters, the preceding premise made a temporary variable _this, you can save the ancestor to the image, the child method can be called with _this, this is the purpose.

JS Common var that = the use of this

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.