JSFiddle - React, Tailwind, and code Playground
by danShumway
JavaScript
//Not a great permanent solution - no typechecking, lots of room for errors.
//
//------------------------------SETUP------------------------------------------------
//This will be copied into every object.
//Let's set up method calls.
Object.prototype.callMethod = function(toCall) {
//Return value
myResults = Array();
//If the proper variables are in place.
if(this.interfaces != undefined) {
//Grab the arguments.
//Also, check it - a legitimate use for the original abuse.
myArguments = Array.prototype.slice.call(arguments, 1);
//Loop through all interfaces, and check to see if the method exists.
//This implementation will call *all* matching methods.
for(var i = 0; i < this.interfaces.length; i++)
{
if(this.interfaces[i].hasOwnProperty(toCall))
{
//Call and push the result into the stack.
myResults.push(this.interfaces[i][toCall].apply(this, myArguments));
}
}
//Return array of *all* results.
return myResults;
}
}
//----------------------END SETUP------------------------------------------------
//----------------------EXAMPLE--------------------------------------------------
//Let's make some interfaces to apply to our object.
//An animal, which can speak.
function Animal(myNoise){
this.noise = myNoise;
this.alive = true;
//All animals can make noise!
this.makeNoise = function(){
alert("I make a noise: " + this.noise);
}
}
//A cyborg which can set its phazors.
function Robot(){
this.phazors = "hug";
//Robots can set their phazors.
this.setPhazors = function(){
alert("Set phazors to " + this.phazors + "!");
}
}
//Lets make an object to inherit from these interfaces.
function CyborgCat(){
//Call both the parents.
Animal.call(this, "meow");
Robot.call(this);
//Just...