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

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools