The code is as follows:
var CachedSearchBox = (function () {
var cache = {},
count = [];
return {
attachSearchBox: function (dsid ) {
(dsid in cache) {//If the result is in the cache
ById(dsid);//New
cache[dsid] = fsb;//Update cache
delete cache[count.shift()];
; if (dsid in cache) {
}
;
var obj2 = CachedSearchBox .attachSearchBox("input1");
Implement encapsulation:
Copy code
The code is as follows:
var person = function(){
//Variable scope is inside the function and cannot be accessed from outside var name = "default"; } }
}();
print(person.name);//Direct access, the result is undefined
print(person.getName());
person.setName("jack") ;
print(person.getName());
The result is as follows:
undefined
default
jack
Another important use of closures is to implement objects in object-oriented. Traditional object languages provide a class template mechanism, so that different objects (instances of a class) have independent members and states without interfering with each other. Although there is no such mechanism as classes in JavaScript, we can simulate such a mechanism by using closures. Let’s take the example above:
Copy the code
The code is as follows:
function Person(){
var name = "default";
return {
getName : function(){
return name;
},
setName: function(newName){
name = newName;
}
}
};
var john = Person();
print(john.getName()) ;
john.setName("john");
print(john.getName());
var jack = Person();
print(jack.getName());
jack.setName("jack");
print(jack.getName());
The running results are as follows:
default
john
default
jack
Problems that JavaScript closures should pay attention to:
1. Memory leaks:
In different JavaScript interpreter implementations, due to defects in the interpreter itself, Using closures may cause memory leaks. Memory leaks are a serious problem, which will seriously affect the browser's response speed, reduce user experience, and even cause the browser to become unresponsive. JavaScript interpreters all have a garbage collection mechanism, which generally adopts the form of reference counting. If the reference count of an object is zero, the garbage collection mechanism will recycle it. This process is automatic. However, with the concept of closure, this process becomes complicated. In closure, because local variables may need to be used at some point in the future, the garbage collection mechanism will not handle these external references. If a circular reference occurs, that is, object A refers to B, B refers to C, and C refers to A, this situation will cause the garbage collection mechanism to conclude that its reference count is not zero, thus causing a memory leak. .
2. Contextual reference:
$(function(){
var con = $("div#panel");
this.id = "content";
con.click(function(){
alert(this.id);//panel
});
});
What value does alert(this.id) here refer to? Many developers may make wrong judgments based on the concept of closure:
content
The reason is that this.id is assigned the value displayed in the click callback, forming The closure will refer to this.id, so the return value is content. However, in fact, this alert will pop up "panel". The reason is this here. Although the closure can refer to local variables, when it comes to this, the situation is a bit subtle, because the existence of the calling object makes When the closure is called (when the click event of this panel occurs), this here refers to the jQuery object con. The this.id = "content" in the anonymous function is an operation performed on the anonymous function itself. The two this references do not refer to the same object.
If we want to access this value in the event handler, we have to make some changes:
$(function(){
var con = $("div#panel");
this.id = "content";
var self = this;
con.click(function(){
alert(self.id);//content
});
});
In this way, what we save in the event handling function is a reference to an external local variable self, not this. This technique has many practical applications, and we will discuss it in detail in the following chapters. We will discuss more about closures in detail in Chapter 9, including discussing "closures" in other imperative languages, the application of closures in actual projects, and so on.
Attachment: Due to my limited level, it is inevitable that there will be omissions and errors in the article, or the language itself may be inappropriate. Timely corrections and suggestions are welcome. This article is just to inspire others, thank you all!