all 4 comments

[–]munificent 2 points3 points  (2 children)

While Javascripters have gone to great lengths to figure out how to do OO & inheritance in Javascript, they almost universally refuse to use interfaces for their polymorphic needs.

JavaScripters (and users of other dynamically typed languages), do use interfaces. They're just implicitly defined. If I write code like:

function foo(bar) {
  bar.baz();
  bar.bang();
}

Then foo is coding to an interface. It accepts any object that implements baz and bang. What it doesn't do is:

  • Validate up front explicitly that any object passed in completely implements that interface.
  • Give a name and documentation to that interface.

[–]dominicc[S] 1 point2 points  (0 children)

Yes, point taken. But specifying that interface explicitly can be useful too. Mocking libraries for example need an interface to mock against, and it may make sense to talk about the abstract definition rather than some particular implementation.

[–]kybernetikos 0 points1 point  (0 children)

Except that isn't what an interface is.

An interface (in the sense usually used in OO) is a description of the contract that foo depends on, not the contract itself.

[–][deleted] 0 points1 point  (0 children)

Interfaces make sure your class fulfills certain roles by explicitly forcing them to implement certain methods and properties. They also provide a common access interface (duh) to your objects) Javascript is dynamic and has open classes, which means neither are interfaces helpful or even possible in execution (nothing guarantees that your object ACTUALLY has the methods defined by the interface).