Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

Question about JavaScript scope for call method

$
0
0
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:


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.

Viewing all articles
Browse latest Browse all 14343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>