1

I would like to create an interface in Javascript, does anyone have any idea how to do this?

In PHP, I can for example create an interface for multiple databases:

interface Database{
    function connect($dsn,$user,$pass,$option);
    function prepare($query);
    function find($table,$column,$condition);
    function save($table,$column,$condition);
}

class MySQL implements Database{
    function connect($dsn,$user,$pass,$option){ ... }
    function prepare($query){ ... }
    function find($table,$column,$condition){ ... }
    function save($table,$column,$condition){ ... }
}

class Oracle implements Database{
    function connect($dsn,$user,$pass,$option){ ... }
    function prepare($query){ ... }
    function find($table,$column,$condition){ ... }
    function save($table,$column,$condition){ ... }
}

Later, when I need to query a table, I can simply execute:

$db = new MySQL;
$db->find('table','column','condition');
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Lewis
  • 14,132
  • 12
  • 66
  • 87
  • Not a real answer, but you should have a look at Typescript if you want to do stuff like this in javascript. – Holt Apr 30 '14 at 17:45
  • Javascript is very primitive, you cant create "interfaces", how you would adapt it on an Object? – Adrian Preuss Apr 30 '14 at 17:45
  • @arjan All those answers seem to miss giving an example of the simple construction of an appropriate objects. While it is the same topic, the questions, and thus appropriate answers, are on vastly different levels. – aaaaaaaaaaaa Apr 30 '14 at 18:13
  • @eBusiness: That's because in duck typing it is completely irrelevant *how* these objects were *constructed*. It could be made from an object literal, instantiated by a constructor, spilled out from a factory, or even be a native object. It only matters what they look like. – Bergi Apr 30 '14 at 18:19
  • @Bergi But you still have to construct the objects one way or another to get to that point, and if OP isn't clear on that process an example is warranted, which the aforementioned question does not provide. – aaaaaaaaaaaa Apr 30 '14 at 18:32
  • @eBusiness OP did not ask how to construct an object with an interface, he asked "how to create an interface" - to which the answer is "you don't need interfaces". If he had asked the first question, he would need to provide more details on what he needs this for, as I said there are so many different ways in JS to create objects and each comes with its own advantages and disadvantages. – Bergi Apr 30 '14 at 18:43

3 Answers3

1

You may simply make an object with the set of functions, you can then substitute another object with the corresponding functions for another database.

dbInterface={
    search:function(searchstring){
        this.oracleDB.somethingDo(searchstring)
    }
    ,oracleDB=new Oracle("foobar")
}
//dbInterface is now an object with the method "search".

In this case this refer to the object the function is attached to

A common approach is to build these objects using a constructor function that also serve as a closure, and thus provide a convenient local scope:

function MySQLinteface(initialization, parameters){
    var dbConnection=connect(parameters)
    this.search=function(searchstring){
        dbConnection.doSomething(searchstring)
    }
}
dbInterface=new MySQLinteface("foo", "bar")
//dbInterface is now an object with the method "search".

In this case this is the return object of the constructor, because it is called with new keyword.

In both cases the crucial part is that you get an object that expose the method search. You don't have to formally define an interface, you just have to make up your mind about how you want your interface to be, then you can reimplement it in any way you like and have all those implementations be plug-in replacements for one another.

aaaaaaaaaaaa
  • 3,630
  • 1
  • 24
  • 23
1

I've made a function which you can determine as an interface:

var MySQL = function(){
    DatabaseInterface(this);
};
MySQL.prototype.connect = function(dsn,user,pass,option){
    //
};
MySQL.prototype.prepare = function(query){
    //
};
MySQL.prototype.find = function(table,column,condition){
    //
};
MySQL.prototype.save = function(table,column,condition){
    //
};
new MySQL;

function DatabaseInterface(con){
    if(!'connect' in con || !getParamNames(con.connect) == "dsn,user,pass,option") 
        throw new Error("This method is not correctly overwritten1!");
    if(!'prepare' in con || !getParamNames(con.prepare) == "query")
        throw new Error("This method is not correctly overwritten2!");    
    if(!'find' in con || !getParamNames(con.find) == "table,column,condition")
        throw new Error("This method is not correctly overwritten3!");
    if(!'save' in con || !getParamNames(con.save) == "table,column,condition")
        throw new Error("This method is not correctly overwritten4!");
}
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
function getParamNames(func) {
    var fnStr = func.toString().replace(STRIP_COMMENTS, '')
    var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(/([^\s,]+)/g)
    if(result === null)
        result = []
        return result
}

In the constructor of you're mysql class you say call the DatabaseInterface function. That function checks if it is a correct database connector. If not he throws an error. This is strange but it works as expected.

But look at the answer of eBusiness and all the other comments. There are no interfaces in javascript and you don't need it in most cases.

EXAMPLE

ArjanSchouten
  • 1,360
  • 9
  • 23
0

JavaScript is not statically typed, so cannot declare any interfaces. You just create objects that fulfill contracts (and if you need, you can dynamically test whether an object provides such methods). This is called duck typing - if an object has connect, prepare, find and save methods we consider it to be a database interface. There are various helper function libraries to ease the declaration/creation of such tests.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375