1.jQuery no new Build
What we want to do is: we just have to write var $ = jQuery (), we can create a new tool object, and we can get the same effect if we want var $ = new JQuery ().
The key to doing this is to write all the tool methods in Jquery.prototype, and return and use the same object as new when building without new.
What do you do to return the same object?
The first thing you should think of when returning an object is return new xxx ();
So where does this xxx () be written?
Should be written in Jquery.prototype, because we want to return the object is able to manipulate all the tools and properties, so the location of xxx should be in the jquery.prototype inside
- To implement no new build, the jquery object itself must first be a function
- This function returns an object generated by an Init constructor in its own prototype.
- The returned object should be a Jquery.prototype object (but not the same object)
- But the problem is that this point in return this in the Init constructor is not what we want Jquery.prototype
- The workaround is simple, just point the prototype of Init to Jquery.prototype, and the object returned by the function jquery can get all the properties and methods of Jquery.prototype
2. Chained calls
Very simply, all methods return this after the operation is complete.
3. Plug-in interface
The so-called plug-in interface is actually the Extend method, this method is not used to specifically access plug-ins.
This method is only used to extend the properties and methods of an object.
Both jquery and Jquery.fn have this approach (implementation is exactly the same), where this method of jquery is often used to extend the object, and this method on the Jquery.fn is used to access the new method of the plugin.
There are a few things to note about the implementation of extend:
jquery Source Reading Program-August 3-1/40