bhartsb
bhartsb

Reputation: 1366

understanding Dojo closure example

In the Dojo docs there is an example:

require([
"dojo/query",
"dojo/_base/array",
"dojo/dom-construct",
"dojo/domReady!"
], function(query, array, domConst){
function topLinks(){
    var headings = query('h2,h3');

    array.forEach(headings, function(elm){
        var topLink = domConst.create("a", {
            href: "#top",
            innerHTML: "^top"
        });

        domConst.place(topLink, elm, "before");
    });
}
});

I assume that function(elm) is a closure with 'elm' being an abbreviation for element, and in this case 2 closures are created, with elm = h2 for the first closure, and elm = h3 for the second closure. Is this correct?

Upvotes: 0

Views: 83

Answers (1)

Ken Franqueiro
Ken Franqueiro

Reputation: 10559

Like CSS, dojo/query supports grouped selectors. h2,h3 will match all h2 and h3 elements, and headings will be a dojo/NodeList with all h2 and h3 elements found in the document. array.forEach simply iterates through the entire NodeList in order, so each time through the function elm references the next h2 or h3 element.

Upvotes: 1

Related Questions