Objective
While jquery is relatively simple, but to fully grasp, and fast and flexible use of it is not so easy, it provides a lot of methods, including the various aspects of the development of the Web page, so to fully grasp these points of knowledge, personally think or need to have a deep understanding of jquery, these knowledge points do sorting memory, This way you will not be confused when you face some jquery code to know how to implement a specific effect is best practice to quickly use jquery for project development.
One of the most common methods in jquery is that jQuery( ) $( ) .
jQuery( )is a function call, and the result of the call is to return a jquery instance object.
Writing a component usually involves encapsulating the component as an object and creating an instance with the new operator when it is needed. But jQuery( ) it does not need to be instantiated manually with new, it automatically returns an instance.
The most straightforward way to achieve this is to define a function:
The problem with this is that there is a dead loop:
The simplest solution is to use another constructor:
There is no technical problem with this, but the jquery author did not do it, probably for some sort of technical cleanliness or for reasons I don't know for a moment, it init was defined in the prototype of the jquery function:
By using Init as a staging point, return a jquery example was finally developed. Looks more "elegant", more "technical fan".
Writing the above code in a self executing function (forming a private scope to avoid namespace contamination) constitutes the core framework of jquery (simplified version):
To understand how the above structure works, you must understand JavaScript's inheritance patterns based on constructors and prototypes.
When the keyword new appears before the function call expression, the function becomes a constructor, and four things happen in turn:
1. First, an empty object (also known as an instance) is created.
2, the null object inherits the properties and methods in the constructor's prototype. This is why the method is written in the constructor prototype .
3, the empty object is assigned to the object within the constructor this .
4. Execute the constructor function. If an object is explicitly returned in the constructor, new it is no longer the newly created empty object, but the return specified object. Otherwise, all returns the new empty object.
See the Nanyi tutorial specifically: http://javascript.ruanyifeng.com/oop/basic.html
jQuery( )The construction principle is clear: each call jQuery( ) returns init the object returned by the constructor this , and the this object has been assigned to the newly created empty object. Since all jQuery.prototype are assigned, the init.prototype newly created empty object inherits all jquery methods.
There is init return this no harm in actually deleting the constructor, as to why the author should add the sentence, er, probably because I know too much.
Summarize
The above is the jquery () method constructs the principle the whole content, hoped this article content to everybody's study or the work may bring certain help, if has the question everybody may the message exchange.