[Original] jQuery's this and $ (this), original jquerythis

Source: Internet
Author: User

[Original] jQuery's this and $ (this), original jquerythis

There are a lot of introductions about jQuery's this and $ (this) on the Internet. Most of them just clarify the points of this and $ (this). In fact, they have application sites, it cannot be generalized. When jQuery calls a member function, this indicates a dom object.

 

$ (This) points to a jQuery object, but this is to point to a dom object, because jQuery has made special processing.

 

When creating a dom jQuery object, jQuery not only creates a jQuery object for the dom, but also stores the dom in the array of the created object.

 

Js Code
  1. Elem = document. getElementById (match [2]);
  2. If (elem & elem. parentNode ){
  3. This. length = 1;
  4. This [0] = elem;
  5. }
  6. This. context = document;
  7. This. selector = selector;
  8. Return this;

 

This [0] = elem is an array of implemented objects. Therefore, javascript is a very interesting language. When using this for access, you can access the member functions of the objects it points to. In fact, this is an array of objects. It stores dom objects.

 

First look at $ ("p"). each () -- loop

 

 

Js Code
  1. Each: function (callback, args ){
  2. Return jQuery. each (this, callback, args );
  3. }

 

After reading the call of the each function, you should understand that jQuery. each (this, callback, args); calls the object array, and the object array stores the dom object. Therefore, this in the callback function is naturally a dom object.

 

 

Let's take a look at $ ("p"). hide () -- member function

 

 

Js Code
  1. Hide: function (){
  2. Return showHide (this );
  3. },
Js Code
  1. Function showHide (elements, show) {var elem, display,
  2. Values = [],
  3. Index = 0,
  4. Length = elements. length;
  5. For (; index <length; index ++ ){
  6. Elem = elements [index];
  7. If (! Elem. style ){
  8. Continue;
  9. }
  10. Values [index] = jQuery. _ data (elem, "olddisplay ");
  11. If (show ){
  12. // Reset the inline display of this element to learn if it is
  13. // Being hidden by cascaded rules or not
  14. If (! Values [index] & elem. style. display = "none "){
  15. Elem. style. display = "";
  16. }
  17. // Set elements which have been overridden with display: none
  18. // In a stylesheet to whatever the default browser style is
  19. // For such an element
  20. If (elem. style. display = "" & isHidden (elem )){
  21. Values [index] = jQuery. _ data (elem, "olddisplay", css_defaultDisplay (elem. nodeName ));
  22. }
  23. } Else {
  24. Display = curCSS (elem, "display ");
  25. If (! Values [index] & display! = "None "){
  26. JQuery. _ data (elem, "olddisplay", display );
  27. }
  28. }
  29. }
  30. // Set the display of most of the elements in a second loop
  31. // To avoid the constant reflow
  32. For (index = 0; index <length; index ++ ){
  33. Elem = elements [index];
  34. If (! Elem. style ){
  35. Continue;
  36. }
  37. If (! Show | elem. style. display = "none" | elem. style. display = ""){
  38. Elem. style. display = show? Values [index] | "": "none ";
  39. }
  40. }
  41. Return elements;
  42. }

From the code above, we can see that the number of lines in hide actually calls showHide, and the first input parameter this is not a dom object, but an array of jQuery objects, therefore, the showHide function obtains each dom object by looping through this object array.

 

Finally, let's take a look at $ ("p"). bind () -- event

 

Js Code
  1. Bind: function (types, data, fn ){
  2. Return this. on (types, null, data, fn );
  3. },

 

 

 

Js Code
  1. On: function (types, selector, data, fn,/* INTERNAL */one ){
  2. // This part of code is omitted
  3. Return this. each (function (){
  4. JQuery. event. add (this, types, fn, data, selector );
  5. });
  6. },

 

The bind function calls the on function, and the on function implements jQuery. event. add through the each function. Therefore, jQuery. event. add (this in this is the dom object. So this in the event is the dom object.

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.