(turn) Overall grasp Jquery-jquery's prototype diagram

Source: Internet
Author: User

overall grasp of the prototype diagram of Jquery-jquery

(formerly) http://www.html5cn.org/article-6529-1.html

2014-7-2 17:12| Publisher: HTML5CN | From: Blog Park | comments: 0

  Summary : A graph showing the relationships between objects in jquery is the most intuitive way many people want to summarize jquery. In this article, a painting shows the core content of the whole jQuery and explains it one after the other. ...  
A few months ago, when I saw an article that was very simple, it was a picture of the relationship between the objects in jquery, and that was the most intuitive way I wanted to summarize jquery. In that article, there are a lot of people who are puzzled, said to see not understand. Here I also re-painted a picture, and explained. The attachment provides a file in Visio format.  

The following will be combined with the JQuery source code, each part of the explanation. In the figure, the circle number represents the location of the JQuery source code, where the circled numbers indicate that these places are from the same block.
  1. /** 
  2. *① What is JQuery?  
  3. * window is an object, it has two attributes, jquery and $ respectively, and its value is a function, the name of this function is jquery. In the function
  4. * In the definition of entities, in fact, through the JQuery.fn.init function to construct the object, the INIT function is the real constructor. In other words,
  5. * What we get through ${...) is actually an example of jQuery.fn.init.  
  6. */
  7. var jQuery = Window.jquery = window.$ = function (selector, context) {
  8. The JQuery object is actually just the Init constructor ' enhanced '
  9. return new JQuery.fn.init (selector, context);
  10. };
Copy Code
  1. /** 
  2. *② 's so-called JQuery's physique
  3. * The JQuery function has two properties, FN and prototype, respectively, and its value is an anonymous object (JSON object).  
  4. */
  5. Jquery.fn = Jquery.prototype = {
  6. Init:function (Selector, context) {
  7. ...
  8. },
  9. jquery: "1.2.6",
  10. Get:function (num) {
  11. ...
  12. },
  13. ...
  14. }
Copy Code
  1. /** 
  2. *③ give JQuery power.
  3. * This code doesn't look like a word, it's short, isn't it? But this piece of code is very important.  
  4. * First, Init is just a function, constructed by JQuery.fn.init, just an instance of JQuery.fn.init,
  5. * Jquery.fn said before, it is an anonymous JSON object, including Init and so on, we want to let JQuery.fn.init construct
  6. * Prototype is one of the best ways to come out with a lot of behavior. Let's say we define a function, named Car.
  7. * Well, the way we use prototype to add behavior to it is familiar to us:
  8. * Function Car (owner) {
  9. * This.owner = owner;  
  10. * } 
  11. * Car.prototype = {
  12. * Go:function () {...}  
  13. * Brake:function () {...}  
  14. * } 
  15. * In this way, objects constructed from new Car () have the go and brake behavior.  
  16. * Therefore, because of the following sentence, the object that was constructed by Init has all the behaviors defined by JQUERY.FN, so handsome!  
  17. */
  18. Give the init function The JQuery prototype for later instantiation
  19. JQuery.fn.init.prototype = Jquery.fn;
Copy Code
  1. /** 
  2. *④ give me the freedom to expand, okay?
  3. * Admittedly, jQuery or Jquery.fn defined function you might not feel enough, it's okay, John Resig provided us with a
  4. * Expand the space. Both JQuery and Jquery.fn have a property named Extend, and the type of the property is a function,
  5. * This function itself has provided us with extended functionality.  
  6. * The function that the Extend attribute represents, but 50 lines of code, write too incisive, have time, we can step by step debugging,
  7. * See the magic of John Resig ~
  8. */
  9. Jquery.extend = JQuery.fn.extend = function () {
  10. ......
  11. }
Copy Code  the core of the entire jQuery is the above four blocks of code, such as event handling code, you can find the shadow from the above four blocks of code. Enjoy jquery~

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.