JavaScript, a function in JS, the first sentence var _this = This; Why do you do this?

Source: Internet
Author: User

JavaScript, a function in JS, the first sentence var _this = This; Why do you do this? Here is the source code:

1 The following code is a common site home page, automatically switch span or Tabbar to change the list display content of the key method:2 functionScrolldoor () {3 }4Scrolldoor.prototype = {5Sd:function(menus,divs,openclass,closeclass) {6 var_this = This;//you see clearly, here, why not directly with this, to use _this instead of all the following this, what is the special significance? What happens if I use this? A total of 2 questions ha!7 if(Menus.length! =divs.length)8 {9Alert ("The number of menu layers and content layers is different!"));Ten return false; One }  A  for(vari = 0; i < menus.length; i++) - {  -_this.$ (Menus[i]). Value =i; the_this.$ (Menus[i]). onmouseover =function(){ -  -  for(varj = 0; J < Menus.length; J + +) - {  +_this.$ (Menus[j]). ClassName =Closeclass; -_this.$ (Divs[j]). style.display = "None"; + } A_this.$ (menus[ This. value]). ClassName =Openclass; at_this.$ (divs[ This. value]). style.display = "Block";  - } - } - }, -$ :function(OID) { - if(typeof(OID) = = "string") in returndocument.getElementById (OID); - returnOID; to } +}

question one: I do not know whether the landlord has contacted jquery jquery inside there is a particularly typical example can be illustrated with the role of _this $ ("#btn"). Click (function () {var_this = This;//this and _this both represent the object of "#btn."$(". 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 different objects, and the coder wants _this to represent the original object Problem 2: In fact, not all use _this but as far as I can see, there should be no difference, the landlord of the Code with _ This is probably just for coding specifications.
You give the example gave me a good inspiration, because JS can be nested code layers, may also be nested below a method, reference this will become sub-method control of the object, if the need for superior to the image, in the case of no parameters, the front premise made a temporary variable _this, can save the superior to the image, Sub-methods can be called with _this, this is the purpose.     Perhaps as you said, in my method, there is no nested method in the back, it is probably just for coding specifications.

JavaScript, a function in JS, the first sentence var _this = This; Why do you do this?

Related Article

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: 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.