0

I defined a javascript helper method like this:

(function (kittenHelper, $, undefined) {

 ...

}(window.kittenHelper = window.kittenHelper || {}, jQuery));

I don't really understand what it's doing as I copied it from another SO post, but it works great.

I ended up defining another one later on called biscuitHelper in the same way.

Then I realised they have some common functionality, both have a name and a refresh method and I expect they will have more common functionality going forward.

Is there some way to create a baseHelper that they will both have access to in the same scope as they would access their local functions and properties?

NibblyPig
  • 46,891
  • 62
  • 180
  • 311
  • It sounds like you should be looking into [classes](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) (implementing one class with the common methods and two subclasses that extend that one). – Frxstrem Mar 15 '17 at 16:38
  • Nice link, looks promising. I had no idea classes were a thing in Javascript, but then I guess I learned it long before 2015... – NibblyPig Mar 15 '17 at 16:41
  • They still use the underlying prototype inheritance chain. It is a semantic addition. You might find [this link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) helpful as well to give you some context. – rasmeister Mar 15 '17 at 16:44
  • "*I don't really understand what it's doing*" - then you should [start with that](http://stackoverflow.com/q/2716069/1048572). Once you know the answer, the solution to your problem will be obvious. – Bergi Mar 15 '17 at 18:21

0 Answers0