JavaScript closure issues

Source: Internet
Author: User
Tags closure

closure of a function: a function that has access to a variable in another function scope. Common way: Create another function inside one function.

function Createcomparisonfunction (propertuname) {return
	function (object1,object2) {
		var value1=object1[ Propertuname];
		var value2=object2[propertuname];
		if (value1<value2) {
			return-1;
		} else if (value1>value2) {return
			1;
		} else{return
			0;
		}
	;
}
Closures can only get the last value of any variable in the function. The closure holds the entire variable object, not a particular value.

function Createfunction () {
	var result=new Array ();
	for (Var i=0;i<10;i++) {
		result[i]=function () {return
			i;}
		;
	}
	return result;
}
var funcs = Createfunction ();  
for (Var i=0 i < funcs.length; i++) {  
    document.write (Funcs[i] () + "<br/>");  

The Createfunction () function returns an array. On the surface, it seems that every function should return its own index value, but it is not, in fact, the return value of each function is 10. Because each function's scope chain contains the active objects of the createfunctions () function, they refer to the same variable i. When the Createfunctions () function returns, the value of the variable i is 10, at which point each function references the same variable object that holds the variable i, so each function returns 10.


To create another anonymous function:

function Createfunction () {
	var result = new Array ();
	for (var i = 0, I < i++) {
		Result[i] = function (num) {return
			function () {return
				num;}
			;
		} (i);
	}
	return result;
}
var funcs = Createfunction ();
for (var i = 0; i < funcs.length i++) {
	document.write (Funcs[i] () + "<br/>");

After overriding the previous createfunction () function, each function returns its own different index values. Here, instead of assigning the closure directly to the value, we define an anonymous function and assign the result of the function immediately to the array. The anonymous function here has a parameter num, which is the value that the final function returns. When we call each anonymous function, we pass in the variable i. Because function arguments are passed by value, the current value of the variable i is copied to parameter Num. Within this anonymous function, there is a closure that creates and returns a access to Num. In this way, each function in the result array has its own copy of the NUM variable, so it is possible to return different values.

closure function: One can read the variables inside the function, and the other is to keep the values of these variables always in memory.

Function F1 () {
	var n = 9;
	ADD = function () {
		n + + 1
	}
	function F2 () {
		alert (n);
	}	
	return f2;
}  
var result = F1 (); Result
();//9
ADD (); Result
();//10              
In this piece of code, result is actually the closure F2 function. It runs two times, the first value is 999, and the second value is 1000. This proves that the local variable n in the function F1 is kept in memory and is not automatically purged after the F1 call. Because the closure will make the variables in the function are stored in memory, memory consumption is very large, so can not abuse the closure, otherwise it will cause Web page performance problems, in IE may lead to memory leaks. The workaround is to remove all unused local variables before exiting the function. It is generally set to NULL to be deleted.

function Assignhandler () {  
    var Element=document.getelementbyid ("Someelement");  
    var id=element.id; 
    Element.onclick=function () {  
         alert (ID);  
    };  
    Element=null; 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.