1

I'm learning JavaScript. My background is traditionally in Java and C#. In those environments, I could use interfaces. A lot of times, I'd have a pattern that looked like this:

var implementers = GetForInterface<ISomeInterface>();

foreach (var implementer in implementers)
  {
  implementer.SomeMethod();
  }

My question is, how do I do something similar in JavaScript? The main thing I can't seem to figure out is the GetForInterface part. I'm not sure what this design pattern is called.

Thank you

4
  • 2
    I think you're looking to make use of prototypal inheritance for pseudo-classes (classes and inheritance as you know it doesn't exist in JavaScript) Commented Jul 18, 2015 at 14:29
  • 3
    Well first of all there is really nothing like an "interface" in JavaScript. Don't try and make JavaScript look or act like other languages you know, especially Java or C#. Commented Jul 18, 2015 at 14:30
  • 1
    What language is that code snipped in? It doesn't look like valid Java, so I guess it's C#? More importantly: What is it supposed to do and when do you need that? We might be able to suggest an alternative pattern that can be used in JavaScript. Commented Jul 18, 2015 at 14:37
  • 1
    Check this : jscriptpatterns.blogspot.com/2013/01/javascript-interfaces.html Commented Jul 18, 2015 at 14:38

2 Answers 2

3

There is no Interfaces in Javascript,

The design pattern you are talking about is service locator, but usually this is not the way to go on javascript .

you could use some library to do this : service-locator or implement it your self with classes and inheritance .

a simple example to go is to hold an array of instances that inherit from the same class and you will be able to loop over them ( and activate the same method for all as desired )

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer except JS doesn't really have classes either...yes I know that "classes" are coming in ES6 but they're just syntactic sugar over prototypes. So it would be more accurate to say "instances that inherit from the same prototype".
you could use function and prototype to implement 'classes' (using the new keyword) and inheritance, you dont need to use the features of ES6 for that
My point is that they're still not really classes. The semantics are completely different from class-based languages.
i can agree with you about this . :)
0

The simplest way I've seen is to use stubs and override them as needed. For example:

     function getter(obj){}

     function objChecker()
       {
       try
         {
         /* Check argument length and constructor type */
         if (getter.length === 1 && getter.constructor === Function)
           {
           console.log("Correct implementation");
           return true;
           }
         else
           {
           console.log("Wrong implementation");
           return false;
           }
         }
       catch(e)
         {
         return NaN;
         }
       finally
         {
         console.log(JSON.stringify(getter))
         }
       }
 
 getter = getter;
 objChecker();
 getter = [];
 objChecker();
 getter = {};
 objChecker();
 getter = RegExp;
 objChecker();
 getter = Function;
 objChecker();

References

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.