


In short, a closure creates a stack area that has no released resources. In other words, it is an uncontrollable memory space occupation. If it is associated with an event, the JS garbage collection mechanism will not touch this area.
For example: We have a project that needs to implement hundreds of hot spots (a tags) in a div, similar to Taobao store advertising space customization. Then according to the traditional approach, we will make the most typical closure as follows The purpose of using the instance is to change the scope of this and call other methods or properties belonging to the scope within its handler function.
var apply = function() {
this. div = document.getElementById("div's id");
this.hot = this.div.getElementsByTagName("a");
for(var i=0; i
return function() {
me.edit(this);
}
}(this);
}
}
apply.prototype = {
Edit: function(target) {
}
}
The problem here is that every time The loop will write an uncontrollable memory address as described above into the memory. Of course, you can't find it and there is no way to clean it when it is no longer needed. The js recycling mechanism does not know when it will be used. is useless and generates junk addresses. Moreover, when the DOM structure within the div changes, you need to find these a tags again and bind events to them.
Of course you can also add this to a local variable: var me = this; At least this is what you can control. You can set the local variable me to null at any time, and the js garbage collection mechanism will know when to go. Clean up this useless data. But this is not the best solution, and it is estimated that many people will not like this unsightly coding method.
The best solution, of course, is that you don’t need to care about the internal structures, nor declare any variables for any internal elements. Then it is the work of the event proxy. What is an event proxy? That is, you do not need to bind events to each sub-object. You can find the element that currently triggers the event through the bubbling mechanism, and find the final processing function through your own series of rules.
If you use the event proxy pattern, how to implement the requirements described above? As follows:
var apply = function() {
this.div = document.getElementById("div's id");
this.div.onclick = function(me) {
return function() {
var _event = arguments.callee.caller.arguments [0];
var target = _event.target || _event.srcElement;
if(target.tagName == "a")
me.edit(target);
else
return false;
}
}(this); >
Now, we only care about what the container element is, not how many a's there are inside it, whether they have changed, etc. The difference in performance is obvious.
It took me about 10 minutes to write it casually. It’s a bit confusing. I hope it will be useful to some friends. If there are any mistakes, please give me some advice.
auntion / 2011-11-15
mail Auntion@gmail.com
QQ 82874972

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates creating dynamic page boxes loaded via AJAX, enabling instant refresh without full page reloads. It leverages jQuery and JavaScript. Think of it as a custom Facebook-style content box loader. Key Concepts: AJAX and jQuery

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

This JavaScript library leverages the window.name property to manage session data without relying on cookies. It offers a robust solution for storing and retrieving session variables across browsers. The library provides three core methods: Session


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

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
