2

I have an array of nodes that will beautifully be populated with objs

var nodesArray = [];

var obj = function(nodeSpan){
    this.nodeS = nodeSpan;

    this.doThings = function(){
            console.log(this.nodeS);
    };
}

I put lots of obj's into nodeArray

var newObj = new obj(thisIsASpan) ///trust me that's a span
nodesArray.push(newObj);
//etc

I call a map function on each node adding an event listener.

Array.prototype.map.call(nodesArray, function(obj, index) {
        obj.nodeS.addEventListener('click', function(obj, index) {
                nodesArray[index].doThings(); ////Throws error in title.
        });
});

I click on one of the damn spans.

I get error in title, I then walk to break room, I then have breakdown.

christopher clark
  • 2,026
  • 5
  • 28
  • 47

1 Answers1

3

You should not be adding obj and index when you are defining the click, those are being set when the click is being fired. In your case the first argument is the event object and the second is undefined since click only has one argument.

Array.prototype.map.call(nodesArray, function(obj, index) {
        obj.nodeS.addEventListener('click', function(event) {
                nodesArray[index].doThings(); 
        });
});
epascarello
  • 204,599
  • 20
  • 195
  • 236