Read one of jQuery (composition of objects)_jquery
I was very confused about how to write jQuery, especially after using Prototype's $, I couldn't understand jQuery's $ for a while. For current front-end students, jQuery may be the first thing they come into contact with, and they will find it very accustomed and natural.
The API documentation from that time is still stored in my computer, so I’ll post a picture to express my appreciation
During this period, my entry-level teacher was Mo Mo. In fact, he is still one of my admired colleagues to this day. His programming skills are very high and he believes that he has already broken through the limitations of programming languages. When everyone was using Prototype.js, and before jQuery was yet popular in China, he had already introduced jQuery into the project.
Let’s get back to business, the current jQuery version has reached 1.6.1. It has expanded from about 2,000 lines at that time to 9,000 lines. I believe it will break through the 1w line soon. For some simple web pages, introducing jQuery is no longer so lightweight. The study here is version 1.6.1. I will read and write at the same time, and finally write a "mini jQuery" of about 1,000 lines.
The following is the jQuery 1.6.1 code snippet
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
},
...
class2type = {};
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function(selector, context, rootjQuery){
}
}
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
At first glance, it looks like we are using the prototype method to write a class jQuery (alias $), but in fact when we use it, we use the function call $("#id"), not new $("# id").
Identifier jQuery is a function, which creates a new instance of function init and then returns. So far we know that its real constructor is jQuery.fn.init. The writing of jQuery is really weird. It hangs init on the jQuery prototype, which is a bit confusing to read.
jQuery.fn.init.prototype = jQuery.fn; is the key sentence. This sentence assigns the prototype of function jQuery to the prototype of function init. The object returned when calling $("#id") consists of two parts.
1, brought by this in function init (such as this.context);
2, brought by the prototype of function jQuery (such as this.size/this.toArray);
Imitate jQuery and write a
// zchain-0.1 .js
function $(selector){
return new $.prototype.init(selector);
}
$.prototype={
init:function(selector){
if(selector === undefined){
this.length = 0;
return this;
}
if(selector.nodeType==1){
this[0] = selector ;
}else{
this[0]=document.getElementById(selector);
}
this.length=1;
},
css:function(name,value ){
this[0].style[name] = value;
return this;//chain call
},
hide:function(){
var self=this;
setTimeout(function(){
self[0].style.display="none";
},3000);
return this;//chain call
}
}
$.prototype.init.prototype=$.prototype;
For the sake of simplicity, the selector here only passes html element or element id (will be enhanced in the future, but all css selectors will not be implemented) ), the length attribute is attached to this and is assigned a value of 1.
When we call
var obj = $( );
console.dir(obj);
$() is actually meaningless, just to test the composition of obj after the call. The firebug console output is as follows:
length:0;
init:function
attr:function
hide:function
That is, the obj object is composed of this and function $ in function init. Composed of prototype.
Looking at the methods on $.prototype, I only added the css and hide methods. The implementation of these two methods is also extremely simple.
First call css to set the font color to red, and hide it after 3 seconds.
To summarize:
The jQuery object refers to an instance of jQuery.prototype.init, which is simply new jQuery.prototype.init. The type of jQuery.prototype.init here is function, which can be regarded as a class.
The jQuery object consists of the following parts:
1. Properties or methods hanging on this in jQuery.prototype.init.
2, properties or methods hanging on jQuery.prototype.init.prototype.
3. Because jQuery.prototype is assigned to jQuery.prototype.init.prototype, the properties and methods hanging on jQuery.prototype are also part of the jQuery object.
4. Properties or methods extended through jQuery.fn.extend({...}) (mentioned later).
/201106/yuanma/zchain.rar

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

How to loop through Javascript objects? The following article will introduce five JS object traversal methods in detail, and briefly compare these five methods. I hope it will be helpful to you!

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can
