mardi 12 juin 2018

Javascript classes and calling their methods [duplicate]

This question already has an answer here:

I'm creating javascript "classes" using the class keyword. In this class I bind event handlers to certain elements in the DOM. So on keypress an event is fired like so

   $('#' + clientID).keyup(function (event) {
        //Search bar is my instance of the class I'm calling the search function
        timeOutSearch = setTimeout(searchBar.Search, 250, event, searchBar);

    });

searchBar.Search also uses other functions of the search bar class say addItem, etc.

What I would like to do is avoid having to pass around the instance of the class to use these functions.

I am designing my functions incorrectly? I try to avoid using this at all cost in my functions because it seems to lead to mistakes later on down the road. Do I need to just be more precise on what is calling my functions? Do people design an entry point in this classes and then work from there? I'd like to design my classes better so example code that does a great job of this would help. I have been reading design patterns for Javascript as well to help further my learning.

Aucun commentaire:

Enregistrer un commentaire