0

I'm trying to reuse a function from existing webpack module instead recreating it again.

Example code from first module for view#1 looks like:

    SearchPopupView.prototype.highlightUser = function(item)
    {
        var self = this;

        self.selUser(item);
    }    

then in the new module, for view#2 - the same HTML template as view#1, I try to reuse the same function:

 UserSearch = __webpack_require__(/*! View/Popup/UserSearch */ 17)

 SearchAccountPopupView.prototype.highlightAccount = function(item)
 {
    UserSearch.prototype.highlightUser(item);
 }

it will execute function from a first module but will throw an error that

self.selUser is not a function

My question is can I reuse any function which operates on a view#1 elements or just those which are only computing values?

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117

1 Answers1

1

First, I would advice you to take a look into this: JavaScript: Class.method vs. Class.prototype.method

JavaScript: Class.method vs. Class.prototype.method

Second, is the UserSearch, you are obtaining, an object (was defined using the keyword new prior to obtain it)? If so, then you don't need to use the prototype. You can use

 UserSearch.highlightUser(item);

If it is not an object, then you need to instantiate it or you need to change your class to use static methods.

acarlstein
  • 1,799
  • 2
  • 13
  • 21
  • `UserSearch` is not an object. Sounds like I will need to use static methods. Can you provide some example on instantiation of declared function? – JackTheKnife Jul 30 '18 at 14:35
  • 1
    let userSearch = new UserSearch(); userSearch.highlightUser(item); – acarlstein Jul 30 '18 at 16:22
  • Remember that we are missing parts of your code so its harder to give you a pin-point solution. Based on the code you are working on is that we can know how to refactor it to your purpose. – acarlstein Jul 30 '18 at 16:23
  • Yes, it makes a sense. At leas I know where to start. Thanks! – JackTheKnife Jul 30 '18 at 18:39