I’m learning how to make OOP with JavaScript. Does it have the interface concept (such as Java’s interface)?
So I would be able to create a listener…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s no notion of “this class must have these functions” (that is, no interfaces per se), because:
Instead, JavaScript uses what’s called duck typing. (If it walks like a duck, and quacks like a duck, as far as JS cares, it’s a duck.) If your object has quack(), walk(), and fly() methods, code can use it wherever it expects an object that can walk, quack, and fly, without requiring the implementation of some “Duckable” interface. The interface is exactly the set of functions that the code uses (and the return values from those functions), and with duck typing, you get that for free.
Now, that’s not to say your code won’t fail halfway through, if you try to call
some_dog.quack(); you’ll get a TypeError. Frankly, if you’re telling dogs to quack, you have slightly bigger problems; duck typing works best when you keep all your ducks in a row, so to speak, and aren’t letting dogs and ducks mingle together unless you’re treating them as generic animals. In other words, even though the interface is fluid, it’s still there; it’s often an error to pass a dog to code that expects it to quack and fly in the first place.But if you’re sure you’re doing the right thing, you can work around the quacking-dog problem by testing for the existence of a particular method before trying to use it. Something like
So you can check for all the methods you can use before you use them. The syntax is kind of ugly, though. There’s a slightly prettier way:
This is standard JavaScript, so it should work in any JS interpreter worth using. It has the added benefit of reading like English.
For modern browsers (that is, pretty much any browser other than IE 6-8), there’s even a way to keep the property from showing up in
for...in:The problem is that IE7 objects don’t have
.definePropertyat all, and in IE8, it allegedly only works on host objects (that is, DOM elements and such). If compatibility is an issue, you can’t use.defineProperty. (I won’t even mention IE6, because it’s rather irrelevant anymore outside of China.)Another issue is that some coding styles like to assume that everyone writes bad code, and prohibit modifying
Object.prototypein case someone wants to blindly usefor...in. If you care about that, or are using (IMO broken) code that does, try a slightly different version: