G
getsanjay.sharma
Hello to all Javascript programmers out there.
Recently I have been fiddling with closures and have come at a stage
where I need to freeze or capture the values of the variables of the
enclosing or the parent function of a closure which I have been
unsuccessful in doing so far.
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>OK</title>
<script type="text/javascript">
function assign(elem, func)
{
elem.onclick = func();
}
//This works incorrectly
function doS()
{
var arr = document.getElementsByTagName('input');
for(var i = 0, max = arr.length; i < max; ++i)
assign(arr, function() {
return function() {
alert(i);
};
});
}
//And so does this
function doZ()
{
var arr = document.getElementsByTagName('input');
for(var i = 0, max = arr.length; i < max; ++i)
arr.onclick = function() {
alert(i);
}
}
/*
//This works for obvious reasons, but is this the only way?
function doS()
{
var arr = document.getElementsByTagName('input');
for(var i = 0, max = arr.length; i < max; ++i)
{
arr.val = i;
arr.onclick = function() {
alert(this.val);
}
}
}
*/
</script>
</head>
<body id="bodyMain" onload="doS();">
<form action="a.html" method="get">
<div>
<input type="text" id="txtId" value="OK" />
<input type="text" name="txtName" value="txtName" />
<input type="button" value="Submit" />
<a href='#'><input type='button' value='OK' /></a>
</div>
</form>
</body>
</html>
Any suggestions / comments / constructive criticisms / code snippets
would be really appreciated along with a bit of theoretical
explanation if possible.
Thanks and regards,
STS.
Recently I have been fiddling with closures and have come at a stage
where I need to freeze or capture the values of the variables of the
enclosing or the parent function of a closure which I have been
unsuccessful in doing so far.
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>OK</title>
<script type="text/javascript">
function assign(elem, func)
{
elem.onclick = func();
}
//This works incorrectly
function doS()
{
var arr = document.getElementsByTagName('input');
for(var i = 0, max = arr.length; i < max; ++i)
assign(arr, function() {
return function() {
alert(i);
};
});
}
//And so does this
function doZ()
{
var arr = document.getElementsByTagName('input');
for(var i = 0, max = arr.length; i < max; ++i)
arr.onclick = function() {
alert(i);
}
}
/*
//This works for obvious reasons, but is this the only way?
function doS()
{
var arr = document.getElementsByTagName('input');
for(var i = 0, max = arr.length; i < max; ++i)
{
arr.val = i;
arr.onclick = function() {
alert(this.val);
}
}
}
*/
</script>
</head>
<body id="bodyMain" onload="doS();">
<form action="a.html" method="get">
<div>
<input type="text" id="txtId" value="OK" />
<input type="text" name="txtName" value="txtName" />
<input type="button" value="Submit" />
<a href='#'><input type='button' value='OK' /></a>
</div>
</form>
</body>
</html>
Any suggestions / comments / constructive criticisms / code snippets
would be really appreciated along with a bit of theoretical
explanation if possible.
Thanks and regards,
STS.