Photo Credit: Thomas Hawk via Compfight cc
This is an object-oriented Servoy tutorial on how to use multiple inheritance with Servoy. If you read the article on “Prototypal Inheritance”, then you learned all about cloning objects using a standard function called objectPlus. This function cloned an object with all its properties and functions, and returned a new object to us, which we could then modify and use to clone new objects from. We applied this approach to a car dealership, creating a vehicle constructor function with basic properties, modifying it with a car constructor function, and then finally making a sports car object for our sexy corvette.
In this article, we will introduce a new standard method called objectMulti, that takes any number of objects, and makes a new object that contains all the properties and functions from all the original objects. In other words, you could have three separate literal objects, and combine them all into one new object, with this single function. Very slick indeed!
In real programming scenarios, different objects may would contain properties and functions that represent different features and capabilities. We can pick and choose from our library, combining the objects to make one super object that inherits all the properties and functions from the individual objects. It’s kind of like a recipe to make something new from individual ingredients, where the ingredients are the individual objects.
Okay, here is the objectMulti function. Copy and paste this code into your utility library. You will find a use for it, trust me.
function objectMulti(arg) {
var n = {}, stuff, j = 0, len = arguments.length;
for (j = 0; j < len; j++){
stuff = arguments[j];
for (var i in stuff){
if (stuff.hasOwnProperty(i)){
n[i] = stuff[i];
}
}
}
return n;
} First we make the vehicle constructor function. Simple; use and object literal and add some properties. We could add methods to, but let’s not push ourselves, okay? Also, like in the Servoy tutorial on prototypal inheritance, we are not going to concern ourselves with which object has which properties. It really doesn’t matter; I’m adding them and overwriting them for demonstration purposes only; this is not a tutorial on designing objects to handle the needs of car dealerships.
function Vehicle(){
var vehicle = {
name: 'unknown',
make: 'unknown',
color: 'unknown',
mgp: 0
};
return vehicle;
} Then we make the car constructor function. Again, straight forward using an object literal.
function Car(){
var car = {
type: 'unknown'
};
return car;
} Finally, we make the sports car constructor function, and go all out, adding a function that will build a string from the properties we know this object will inherit from car and vehicle.
function SportsCar(){
var sportsCar = {
type: "Sports Car",
premiumPaint: 'unknown',
getInfo: function(){
return this.color
+ " " + this.make
+ " " + this.name
+ " " + this.type
+ " - " + this.mpg + "mpg"
+ " - " + this.premiumPaint;
}
};
return sportsCar;
} Whew! That was tough, right? Not! Okay, let’s use the constructors to build a new Corvette. First we create a new vehicle using the vehicle constructor function. Then we create a new car using the car constructor function. Finally, we build a new sports car using the sports car constructor function. Next, we pass the vehicle object, car object and sports car object, along with values for properties, into objectMulti, which combines them all, returning to us a new Corvette object. Pretty cool, right?
var vehicle = new Vehicle();
var car = new Car();
var sportsCar = new SportsCar();
var myCorvette = objectMulti( vehicle, car, sportsCar, {
name: "Corvette",
make: "Chevy",
color: "White",
premiumPaint: 'Pearl White',
mpg: 10
});
application.output(myCorvette.getInfo());
//White Chevy Corvette Sports Car - 10mpg - Pearl White As you can see, using multiple inheritance is quite easy with a function like objectMulti. Put together objects from your collection and get a new object that inherits from all the objects. It’s very handy in real-world programming situations and works great with Servoy. Enjoy!
This is a Servoy tutorial on how to optimize code performance. A while back, I had…
This is an object-oriented Servoy tutorial on how to use an object as a cache in…
This is an object-oriented Servoy tutorial on how to use function memoization with Servoy. Function memoization…
This is an object-oriented Servoy tutorial on how to use object-oriented programming in Servoy. Javascript’s core…
This is an object-oriented Servoy tutorial on how to use inheritance patterns in Servoy. I use…
This is an object-oriented Servoy tutorial on how to use prototypal inheritance in Servoy. When…