Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are looking at two different things.</p> <p>First you have interfaces. The most accepted way of implementing this is though Duck Typing ("if it looks like a duck and quacks like a duck then it is a duck"). This means that if an object implements a set of methods of the interface then it is that interface. You implement this by having an array of method names that define an interface. Then to check if an object implements that interfece you see if it implements those methods. Here is a code example I whipped up:</p> <pre><code>function Implements(obj, inter) { var len = inter.length, i = 0; for (; i &lt; len; ++i) { if (!obj[inter[i]]) return false; } return true; } var IUser = ["LoadUser", "SaveUser"]; var user = { LoadUser : function() { alert("Load"); }, SaveUser : function() { alert("Save"); } }; var notUser = { LoadUser : function() { alert("Load"); } }; alert(Implements(user, IUser)); alert(Implements(notUser, IUser)); </code></pre> <p>Now you have inheritance. JS has no inheritance built in; so you have to implement it manually. This is just a matter of "copying" the properties of one object to another. Here is another code sample (not perfect but it demonstrates the point):</p> <pre><code>function InheritObject(base, obj) { for (name in base) { if (!obj[name]) obj[name] = base[name]; } } var Base = { BaseFunc : function() { alert("BaseFunc from base"); }, InheritFunc : function() { alert("InheritFunc from base"); } } var Inherit = { InheritFunc : function() { alert("InheritFunc from inherit"); }, AnotherFunc : function() { alert("AnotherFunc from inherit"); } } InheritObject(Base, Inherit); Inherit.InheritFunc(); Inherit.BaseFunc(); Inherit.AnotherFunc(); Base.BaseFunc(); Base.InheritFunc(); </code></pre> <p>You probably want to look at <a href="http://www.mootools.net" rel="nofollow noreferrer">http://www.mootools.net</a>. It has my favorite implementation of Classes. You also definitely want to check out "Pro Javascript Design Patterns"</p> <p><a href="http://rads.stackoverflow.com/amzn/click/159059908X" rel="nofollow noreferrer">http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X</a></p> <p>This book goes into good detail about how to emulate OOP in javascript.</p>
    singulars
    1. This table or related slice is empty.
    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. 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