


Simulation code of jQuery implementation principle -3 Event handling_jquery
In the private extension object of the object, an event management object named events is specially added. Each event on this object corresponds to an attribute with the same name. The value of this attribute is an array. The handler for this event is pressed in sequence. into this array to form a list of event handlers. The custom event handling function is pushed into this list.
When the event is triggered, jQuery.event.handle is executed through the registered anonymous function. Since a closure is used, this in this function is the event source object. The object is found through this event source object. private extended data, and then find the corresponding event handler list in events, and finally execute them in sequence.
///
// #2076
// The id used to generate the event handler function
jQuery.guid = 1;
//jQuery’s event object
jQuery.event = { // # 1555
// Add an event to the object
// elem adds the element of the event, type is the name of the event, handler Event handler, data event-related data
add: function (elem, type, handler, data) {
var handleObjIn, handleObj;
// Confirm that the function has a unique ID
if ( !handler.guid) {
handler.guid = jQuery.guid;
}
// Get the cache data object corresponding to this element
var elemData = jQuery.data(elem);
//Get the event object on the cache object corresponding to the element and the handler common to all events
var events = elemData.events = elemData.events || {};
var eventHandle = elemData.handle;
//Whether there is already only one event processing function handle, jQuery.event.handle is used.
// By using closure, this function refers to the current event object and parameters.
if (!eventHandle) {
elemData.handle = eventHandle = function () {
return jQuery.event.handle.apply(eventHandle.elem, arguments);
};
}
// Allows the closure handler to find the event source object
eventHandle.elem = elem;
//
handleObj = { handler: handler, data: data};
handleObj.namespace = "";
handleObj.type = type;
handleObj.guid = handler.guid;
// Each event can have a series of handlers, in the form of array
var handlers = events[type],
special = jQuery.event.special[type] || {};
// Init the event handler queue
if (!handlers) {
handlers = events[type ] = [];
// Check for a special event handler
// Only use addEventListener/attachEvent if the special
// events handler returns false
// Complete the actual event registration
//The actual event handling function is eventHandle
if (!special.setup || special.setup.call(elem, data, namespaces, eventHandle) === false) {
// Bind the global event handler to the element
if (elem.addEventListener) {
elem.addEventListener(type, eventHandle, false);
} else if (elem.attachEvent) {
elem.attachEvent("on" type, eventHandle);
}
}
}
// The custom handler function is in a stack, and later jQuery.event.handle will find the actual handler
handlers here. push(handleObj);
// Nullify elem to prevent memory leaks in IE
elem = null;
},
global: {},
// Real event handler function,
// Since it is called through return jQuery.event.handle.apply(eventHandle.elem, arguments)
// Therefore, this at this time is the event source object, and event is the event parameter
handle: function (event) { // 1904
var all, handlers, namespaces, namespace, events;
event = window.event;
event.currentTarget = this;
// On the current event object Find the event handler list
var events = jQuery.data(this, "events"), handlers = events[event.type];
if (events && handlers) {
// Clone the handlers to prevent manipulation
handlers = handlers.slice(0);
for (var j = 0, l = handlers.length; j var handleObj = handlers[j];
// Get the parameters saved when registering the event
event.handler = handleObj.handler;
event.data = handleObj.data;
event.handleObj = handleObj;
var ret = handleObj.handler.apply(this, arguments);
}
}
return event.result;
},
// #2020
special: {}
}
//bind function definition
jQuery.fn.bind = function(type, fn)
{
var handler = fn;
//Call jQuery.event.add to add event
for (var i = 0, l = this.length; i jQuery.event.add(this[i], type, handler);
}
return this ;
}
jQuery.fn.unbind = function (type, fn) {
// Handle object literals
if (typeof type === "object" && !type.preventDefault) {
for (var key in type) {
this.unbind(key, type[key]);
}
} else {
for (var i = 0, l = this.length ; i jQuery.event.remove(this[i], type, fn);
}
}
return this;
}
/ / Click event registration method
jQuery.fn.click = function (fn) {
this.bind("click", fn);
return this;
}
In this way, for the element with the id msg on the page, you can register a click event handler function through the following code.
// Event operation
$("#msg ").click(
function () {
alert(this.innerHTML);
}
);

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

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.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.