Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no notion of "this class must have these functions" (that is, no interfaces per se), because:</p> <ol> <li>JavaScript inheritance is based on objects, not classes. That's not a big deal until you realize:</li> <li>JavaScript is an <em>extremely</em> dynamically typed language -- you can create an object with the proper methods, which would make it conform to the interface, <em>and then undefine all the stuff that made it conform</em>. It'd be so easy to subvert the type system -- even accidentally! -- that it wouldn't be worth it to try and make a type system in the first place.</li> </ol> <p>Instead, JavaScript uses what's called <a href="http://en.wikipedia.org/wiki/Duck_typing" rel="noreferrer">duck typing</a>. (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.</p> <p>Now, that's not to say your code won't fail halfway through, if you try to call <code>some_dog.quack()</code>; 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.</p> <p>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</p> <pre><code>if (typeof(someObject.quack) == "function") { // This thing can quack } </code></pre> <p>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:</p> <pre><code>Object.prototype.can = function(methodName) { return ((typeof this[methodName]) == "function"); }; if (someObject.can("quack")) { someObject.quack(); } </code></pre> <p>This is standard JavaScript, so it should work in any JS interpreter worth using. It has the added benefit of reading like English.</p> <p>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 <code>for...in</code>:</p> <pre><code>Object.defineProperty(Object.prototype, 'can', { enumerable: false, value: function(method) { return (typeof this[method] === 'function'); } } </code></pre> <p>The problem is that IE7 objects don't have <code>.defineProperty</code> at 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 <code>.defineProperty</code>. (I won't even mention IE6, because it's rather irrelevant anymore outside of China.)</p> <p>Another issue is that some coding styles like to assume that everyone writes bad code, and prohibit modifying <code>Object.prototype</code> in case someone wants to blindly use <code>for...in</code>. If you care about that, or are using (IMO <em>broken</em>) code that does, try a slightly different version:</p> <pre><code>function can(obj, methodName) { return ((typeof obj[methodName]) == "function"); } if (can(someObject, "quack")) { someObject.quack(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload