I try to understand this example from jquery api
in this snippet
var tags = this.map(function () {
return this.tagName;
})
.get().join(", ");
why get() is necessary?
in the api it says that get() Retrieve the DOM elements matched by the jQuery object. In this case, I see that get() is not applied to jq object, but on strings.
When I remove get(), I get this error message:
Uncaught TypeError: Object [object Object] has no method ‘join’
why join can not work with map?
Here,
thisis jQuery object with a list of nodes. (All functions injQuery.fnhave their context (i.e.,this) set to the jQuery object that is making the call to the function.)The call to
mapreturns a jQuery object with a list of strings. (You may be confusing jQuery’smapfunction with themaparray function introduced in ECMAScript 5,)The call to
getreturns a plain JS array of those strings, andjoinacts on that array.The call to
getis necessary to transform the jQuery object with a list of strings (returned bymap) into a plain JS array so it can be glued together withjoin.