I'm reading about using plain JavaScript instead of jQuery, and came across an article on looping through elements that are in a node list.
This is that article: https://toddmotto.com/ditch-the-array-fo...list-hack/
At the end, there is a recommendation to do the following:
I see the comment regarding scope, but looking around the internet cannot find what this scope variable is all about. Can somebody explain about this scope? I know what scope is in jquery when I do something like this:
But I don't know about the other code.
This is that article: https://toddmotto.com/ditch-the-array-fo...list-hack/
At the end, there is a recommendation to do the following:
Code:
// forEach method, could be shipped as part of an Object Literal/Module
var forEach = function (array, callback, scope) {
for (var i = 0; i < array.length; i++) {
callback.call(scope, i, array[i]); // passes back stuff we need
}
};
// Usage:
// optionally change the scope as final parameter too, like ECMA5
var myNodeList = document.querySelectorAll('li');
forEach(myNodeList, function (index, value) {
console.log(index, value); // passes index + value back!
});I see the comment regarding scope, but looking around the internet cannot find what this scope variable is all about. Can somebody explain about this scope? I know what scope is in jquery when I do something like this:
Code:
var scope = $('#container');
$('#something', scope).whatever();But I don't know about the other code.