Fusengammu
Registered
Hi, one day a long time ago (3 yrs), I was working on Javascript where I really wanted to be able to use Java/C++ style inheritance instead of the built in prototype inheritance, so I came up with a scheme to do this. Now I'm working on some ActionScript in Flash and needed this facility yet again. So, I once again recoded my inheriance hack yet again in ActionScript.
No, I'm not having trouble making it work, but I was wondering if anyone has ever heard or seen of making JavaScript's OO behave like Java's OO. If not, I would like to opensource this method, but don't exactly know where to begin.
It works like this:
File oo.js:
/*
3/14/2003 by Kensuke Arai araikensuke@hotmail.com
original code, from sometime in the year 2000
*/
function OOEvolve(subclass)
{
for( key in subclass.superClass )
{
if( subclass.superClass[key] != null )
{
subclass[key] = subclass.superClass[key];
}
}
// native code doesn't get copied over, so...
sSuperToStringCode = subclass.superClass.toString + "";
sSubToStringCode = subclass.toString + "";
if( (sSuperToStringCode.indexOf("[native code]") < 0) &&
(sSubToStringCode.indexOf("[native code]") >= 0) )
{
subclass.toString = subclass.superClass.toString;
}
}
File OOTest.html:
/***********************/
// MUST INCLUDE using SCRIPT src="oo.js"
/***********************/
// SUPERCLASS Vehicle
function Vehicle(sManufacturer, iWheels, iWeight)
{
this.sManufacturer = sManufacturer;
this.iWheels = iWheels;
this.iWeight = iWeight;
this.toString = Vehicle_toString;
}
function Vehicle_toString()
{
return "Object type vehicle. Manufactured by " + this.sManufacturer +
", has " + this.iWheels + " wheels, and weighs " + this.iWeight;
}
// SUBCLASS
// A type of car. It can be outfitted in several different
// trim levels.
function AustinMini(sTrimLevel)
{
// DECLARE WHAT SUPERCLASS I EXTEND
this.superClass = new Vehicle("Austin", 4, 1700);
OOEvolve(this);
// overloaded toString for AustinMini...
this.sTrimLevel = sTrimLevel;
this.toString = AustinMini_toString;
}
function AustinMini_toString()
{
return "Object type AustinMini. Manufactured by " + this.sManufacturer +
", has " + this.iWheels + " wheels, and weighs " + this.iWeight +
". The trim level is " + this.sTrimLevel;
}
// SUBCLASS
// A type of bicycle. Bicycles come in sizes.
function CannondaleR2000(iSize)
{
// DECLARE WHAT SUPERCLASS I EXTEND
this.superClass = new Vehicle("Cannondale", 2, 16);
OOEvolve(this);
this.iSize = iSize;
this.getSize = Cannondale_getSize;
}
function Cannondale_getSize()
{
return this.iSize;
}
var minicooper = new AustinMini("Cooper");
var stdcooper = new AustinMini("S");
var cannondale = new CannondaleR2000();
alert(minicooper.toString());
alert(stdcooper.toString());
alert(cannondale.toString());
No, I'm not having trouble making it work, but I was wondering if anyone has ever heard or seen of making JavaScript's OO behave like Java's OO. If not, I would like to opensource this method, but don't exactly know where to begin.
It works like this:
File oo.js:
/*
3/14/2003 by Kensuke Arai araikensuke@hotmail.com
original code, from sometime in the year 2000
*/
function OOEvolve(subclass)
{
for( key in subclass.superClass )
{
if( subclass.superClass[key] != null )
{
subclass[key] = subclass.superClass[key];
}
}
// native code doesn't get copied over, so...
sSuperToStringCode = subclass.superClass.toString + "";
sSubToStringCode = subclass.toString + "";
if( (sSuperToStringCode.indexOf("[native code]") < 0) &&
(sSubToStringCode.indexOf("[native code]") >= 0) )
{
subclass.toString = subclass.superClass.toString;
}
}
File OOTest.html:
/***********************/
// MUST INCLUDE using SCRIPT src="oo.js"
/***********************/
// SUPERCLASS Vehicle
function Vehicle(sManufacturer, iWheels, iWeight)
{
this.sManufacturer = sManufacturer;
this.iWheels = iWheels;
this.iWeight = iWeight;
this.toString = Vehicle_toString;
}
function Vehicle_toString()
{
return "Object type vehicle. Manufactured by " + this.sManufacturer +
", has " + this.iWheels + " wheels, and weighs " + this.iWeight;
}
// SUBCLASS
// A type of car. It can be outfitted in several different
// trim levels.
function AustinMini(sTrimLevel)
{
// DECLARE WHAT SUPERCLASS I EXTEND
this.superClass = new Vehicle("Austin", 4, 1700);
OOEvolve(this);
// overloaded toString for AustinMini...
this.sTrimLevel = sTrimLevel;
this.toString = AustinMini_toString;
}
function AustinMini_toString()
{
return "Object type AustinMini. Manufactured by " + this.sManufacturer +
", has " + this.iWheels + " wheels, and weighs " + this.iWeight +
". The trim level is " + this.sTrimLevel;
}
// SUBCLASS
// A type of bicycle. Bicycles come in sizes.
function CannondaleR2000(iSize)
{
// DECLARE WHAT SUPERCLASS I EXTEND
this.superClass = new Vehicle("Cannondale", 2, 16);
OOEvolve(this);
this.iSize = iSize;
this.getSize = Cannondale_getSize;
}
function Cannondale_getSize()
{
return this.iSize;
}
var minicooper = new AustinMini("Cooper");
var stdcooper = new AustinMini("S");
var cannondale = new CannondaleR2000();
alert(minicooper.toString());
alert(stdcooper.toString());
alert(cannondale.toString());