Home > Web Front-end > JS Tutorial > body text

Make good use of event proxies and be wary of the performance traps of closures. _javascript skills

WBOY
Release: 2016-05-16 18:11:24
Original
1223 people have browsed it

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.

Copy code The code is as follows:

var apply = function() {
 this. div = document.getElementById("div's id");
this.hot = this.div.getElementsByTagName("a");
for(var i=0; i this.hot[i].onclick = function(me) {
  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:
Copy code The code is 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
Original article, please leave this part of the information for reprinting
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!