I've got question. I have my own jquery-like library. I call $ or Meh (that's what it's called - the Mehlib) and as parameter passing selectors. This library calls document.querySelectorAll
which returns a nodeList. To work with elements (Nodes) in this list (I mean, call a method for them) i gotta loop them. I've figured out already like this:
$('.selector-class').each(function (el) {
el.style.display = 'none'; // hide element
});
Meh.fn = Meh.prototype = {
map: function (callback) {
var match = [];
for (var i = 0; i < this.length; i++) {
push.call( match, callback.call( this, this[i], i ) );
}
return match;
},
each: function (callback) {
return this.map(callback);
}
};
So, this works fine, but let's consider myself lazy ok?
I want to "save" a current element when looping (mapping) to this
and return it. So the call (the first 3 lines above) would be like this:
$('.selector-class').each(function () {
this.style.display = 'none';
});
So I still want to call a callback function when mapping nodes, but I don't want to save single mapped element to the callback's function parameter.
I've tried to create new instance of library in map method, and to current method save mapped duplicated single nodes like this this = duplicated[i];
but there's an uncaught error : Uncaught reference error: Invalid left-hand side in assignment
and Uncaught reference error: $ is not defined
error too.