I need help comparing two similar code snippets:
myFunc();
function myFunc() {
var e = document.getElementByClassName("link"),
i = e.length;
while (i--) {
e[i].addEventListener("click", function() {
//do stuff for each a.link
}, false);
}
}
And
function myFunc() {
//do stuff for each a.link
}
var e = document.getElementByClassName("link"),
i = e.length;
while (i--) {
e[i].addEventListener("click", function() {
myFunc()
}, false);
}
The first one allows me to use this
like
var c = this.getAttribute("href")
to get the attribute of a.link
.
However, the second one seems to have better performance since it calls myFunc()
only when a.link
is clicked
Which approach would be more efficient in terms of page speed?
EDIT
myFunc
will be utilized multiple times during ajax calls.