JSFiddle - React, Tailwind, and code Playground
by andersand
JavaScript
/*
* Experimenting with javascript interface mimicking.
*/
Function.prototype.inherits = function (ParentClass) {
this.prototype = new ParentClass();
this.prototype.constructor = this;
return this;
};
Function.prototype.implement = function(Interface) {
var interfaceInstance = new Interface();
var thisInstance = new this();
var interfaceProp, thisProp;
for (interfaceProp in interfaceInstance) {
if (!thisInstance.hasOwnProperty(interfaceProp)) {
// TODO improve this logging: class xxx does not implement property xxx of interface xxx
console.error("Interface not fully implemented! Interface property \"" + interfaceProp + "\" missing from class");
}
}
for (thisProp in thisInstance) {
if (!interfaceInstance.hasOwnProperty(thisProp)) {
// TODO improve this logging: class xxx implements property xxx which is not defined in interface xxx
console.error("Class implements public property \"" + thisProp + "\", which is not defined in its interface!");
}
}
};
// setting up namespace
window.codio = {};
window.codio.services = {};
(function(ns) {
console.log("Setting up services");
/**
* The meeting app service interface
*/
function IMeetingService() {
/**
* @param username string
* @return json { name, email, department }
*/
this.getUser = function(username) {
};
/**
* @param user json { name, email, department }
* @return void
*/
this.saveUser = function(user) {
};
};
ns.MyService = (function MyService() {
var a = "a";
function b () {
}
this.c = "public";
this.getUser = function() {
};
}).implement(IMeetingService);
})(window.codio.services);