0

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.

  • 1
    Why reinventing the wheel? There are many well-tested DOM libraries (light and heavy) that work like jQuery. If the point is _learning_ then that's a different story, otherwise don't waste your time. – Ram Jan 03 '16 at 16:57
  • Because I am developing a **thing** that is **LIGHT** as you said, and not a dozens of kB. Why would you build own car (if you would have a means) if there are many cars ? Because you want your own that you know from a screw to roof and you are interesting in it. – Tralala lalala Jan 03 '16 at 17:00
  • 2
    jQuery is light for what it does, and is extensively tested and well documented and written by people that have an intimate understanding of javascript and the DOM. Yours has nothing of this especially if you are asking such a basic fundamental question about object scoping. –  Jan 03 '16 at 17:04
  • Okay. When you are not about to help, don't say anything. jQuery is light for what it does, you are right, but maybe I don't need such functionality and what if I need a small size of script? So don't be the smarter than you really are. **EDIT: ** And I am learning if you would like to know. This is for my practical test in school and there are quotas. You cannot bring a website that has no content but 100kB size. – Tralala lalala Jan 03 '16 at 17:11

2 Answers2

0

You do not assign to this.

However, you can define what this is in the context of calling a function.

Namely:

someObject.someMethod(); // "this" is someObject
someFunction(); // "this" is typically "window" but may be "null"

someFunction.call(someThing, arg1, arg2); // "this" is someThing

In the last one there, you are providing whatever you want this to be when calling the function.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Oh.. So i didn't understand to function call() .. so the first param is a value of `this` ? – Tralala lalala Jan 03 '16 at 17:05
  • Correct. When using `.call()`, the first argument will be `this`, then all following arguments will be passed to the function itself. `.apply()` is similar but it takes `this` followed by an *array* of values to pass as arguments. – Niet the Dark Absol Jan 03 '16 at 17:06
  • When I call `funct( arg1, arg2 )` and `funct.call(someThing, arg1, arg2)`, then in both will be arg1 equal and arg2 equal too? – Tralala lalala Jan 03 '16 at 17:08
  • @Tralala lalala That's right, the first argument becomes the `thisArg` the rest are the normal arguments in order. – Paul Jan 03 '16 at 17:27
0

Change:

callback.call( this, this[i], i )

To:

callback.call( this[i], this[i], i )

So that you are passing in the element as the thisArg.

Paul
  • 139,544
  • 27
  • 275
  • 264