Javascript OOP Interfaces!

Discussion in 'JavaScript' started by mattprokes, Nov 18, 2008.

  1. #1
    So today I am reading through this book pro-javascript design patterns, excellent book by the way! I had come upon chapter 2, which was an excellent explanation of interfaces. One thing that concerned myself is that the book states that it is impossible to create enforceable interfaces in javascript. I pondered that for a bit, as far as what interfaces really are. If you look at interfaces from the perspective of java, then for the most part they are types, with a specified subset of methods for a class instance.

    Thinking in those terms, I sat back and pondered the problem again, sooo all I need to do really to create interfaces that are enforceable in javascript would be to separate the concept of an interface from a class all together and make it a class its self! WALA! True first class interfaces in javascript!

    I have an example of oop interfaces in javascript here! mattprokes.com/category/js-interfaces/
     
    mattprokes, Nov 18, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Interesting code but I don't think this solves the problem of having interface behavior. Interfaces seem more useful at compile time than runtime, which means they're probably not so useful in javascript. But maybe if you threw an error when it's casted it would be more useful.

    With YUI I think you could imitate abstract classes by using YAHOO.lang.extend and then in the abstract methods in the super class throw your error there.
    I guess this could be done with any framework that offers inheritance, or you could do it yourself. I think this has the same end effect as your code does.

    I took the example from http://developer.yahoo.com/yui/yahoo/#extend and just added an abstract method.

    This has the same effect because it doesn't error until you call the method.

    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"><html>
     <head>
      <title> YUI Abstract Class Test </title>
    
    	<!-- YAHOO Global Object source file -->  
    	<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js" ></script> 
    	<!-- Additional source files go here --> 
    
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    YAHOO.namespace("test"); 
    	 
    	YAHOO.test.Class1 = function(info) { 
    	    alert("Class1: " + info); 
    	}; 
    	 
    	YAHOO.test.Class1.prototype.testMethod = function(info) { 
    	    alert("Class1: " + info); 
    	}; 
    	 
    	YAHOO.test.Class1.prototype.abstractMethod1 = function(info) { 
    	    throw "Must implement abstractMethod1 in subclass"; 
    	}; 
    	 
    
    	YAHOO.test.Class2 = function(info) { 
    	    // chain the constructors 
    	    YAHOO.test.Class2.superclass.constructor.call(this, info); 
    	    alert("Class2: " + info); 
    	}; 
    	 
    	// Class2 extends Class1.  Must be done immediately after the Class2 constructor 
    	YAHOO.lang.extend(YAHOO.test.Class2, YAHOO.test.Class1); 
    	 
    	YAHOO.test.Class2.prototype.testMethod = function(info) { 
    	    // chain the method 
    	    YAHOO.test.Class2.superclass.testMethod.call(this, info); 
    	    alert("Class2: " + info); 
    	}; 
    	 
    	var class2Instance = new YAHOO.test.Class2("constructor executed"); 
    	class2Instance.testMethod("testMethod invoked"); 
    	class2Instance.abstractMethod1(); 
    
    //-->
    </SCRIPT>
    
     </head>
    
     <body>
      
     </body>
    </html>
    
    
    Code (markup):
     
    LogicFlux, Nov 19, 2008 IP