Hm, I disagree. I pasted the above code, using default settings on the jslint page, and I got:
Error: Implied global: doSomething 1, document 3
and
Global elements, i, onclickGenerator
1 onclickGenerator(i)
1 "return"()
Outer i
Global doSomething
Yeah, building multiple functions is a bit wasteful. I suppose you could get by by attaching new properties to the elements and having the function attached to onclick reference the objects by using the event's target/srcElement property to get back to the custom variable.
Well.. wastefull.. true, but for some apps it is detrimental (think ajax). Your example can perhaps be converted into something which avoids the need to pass "i" at all. Script libraries like YUI have code where you can control scoping when the handler executes. This may be overkill in this situation. So in this example perhaps setting an (html) id on the element and extracting it in the "doSomething" function would suffice, then you don't need the parameter at all.
function doSomething() {
var id = this.id/* lets say id is a1*/
doSomethingElse(id);
}
then (something like)
for (var i = 0; i < elements.length; i++) {
elements[i].id = "a"+i;
elements[i].onclick = doSomething; }
But I really do recommend the screencasts, I thought I knew stuff about javascript, after having it explained (and verifed) in the screencats it is much easier to simply "use the good parts of javascript".
If you would read the book you would find that your code contains a few definite "don't do that, that is bad practice" statements (at least according to Crockford).
If we take away the fact that there are several global variables created, the most problematic thing is that the code creates "elements.length" number of functions -> memory consuming, when it is easy to avoid.
There are several videos on YDN theatre which discuss many of the subjects the reviewer mentions (these screencasts came before the book). After I watched one I ended up watching just about all of them. See and listen to http://developer.yahoo.net/blogs/theater/archives/douglas_crockford/
and you will start to understanding javascript... not just write stuff what seems to work.
If you want to see if you write "good" javascript try "jslint" written by Crockford http://www.jslint.com/ But like the site says "WARNING: JSLint may hurt your feelings."
Error: Implied global: doSomething 1, document 3
and
Global elements, i, onclickGenerator
1 onclickGenerator(i)
1 "return"()
Outer i
Global doSomething
Yeah, building multiple functions is a bit wasteful. I suppose you could get by by attaching new properties to the elements and having the function attached to onclick reference the objects by using the event's target/srcElement property to get back to the custom variable.
Well.. wastefull.. true, but for some apps it is detrimental (think ajax). Your example can perhaps be converted into something which avoids the need to pass "i" at all. Script libraries like YUI have code where you can control scoping when the handler executes. This may be overkill in this situation. So in this example perhaps setting an (html) id on the element and extracting it in the "doSomething" function would suffice, then you don't need the parameter at all.
/* lets say id is a1*/
function doSomething() {
var id = this.id
doSomethingElse(id);
}
then (something like)
for (var i = 0; i < elements.length; i++) {
elements[i].id = "a"+i;
elements[i].onclick = doSomething; }
But I really do recommend the screencasts, I thought I knew stuff about javascript, after having it explained (and verifed) in the screencats it is much easier to simply "use the good parts of javascript".
If you would read the book you would find that your code contains a few definite "don't do that, that is bad practice" statements (at least according to Crockford).
If we take away the fact that there are several global variables created, the most problematic thing is that the code creates "elements.length" number of functions -> memory consuming, when it is easy to avoid.
There are several videos on YDN theatre which discuss many of the subjects the reviewer mentions (these screencasts came before the book). After I watched one I ended up watching just about all of them. See and listen to http://developer.yahoo.net/blogs/theater/archives/douglas_crockford/
and you will start to understanding javascript... not just write stuff what seems to work.
If you want to see if you write "good" javascript try "jslint" written by Crockford http://www.jslint.com/ But like the site says "WARNING: JSLint may hurt your feelings."