R
Robert Spoons
Can you look over this code, preferably try it, and comment?
I believe the 'extend' function below will allow you to use full
'class inheritance' in javascript, but I would like to verify it.
The extend function allows the following:
1) inheriting from multiple classes,
2) inheriting an inherited classes inheritances (awkward to say),
3) inheriting appended prototype methods and properties of inhertited
classes.
Thanks.
//----------------------------------------------------------
//rspoons
//'extend' must have two arguments, the object to inherit and
//an Array of arguments to supply the superclass.
//If the superclass requires no arguments, [null] must be passed.
Object.prototype.extend=function(){
if(arguments[1]){ //make sure argument(s) are supplied
for(var i=0;i<arguments[1].length;i++){
if(typeof(arguments[1])=="string")
arguments[1]="'"+arguments[1]+"'"
}
if(arguments[1].length>0){Arguments=arguments[1].join(",")}
else{Arguments=arguments[1][0]}
for(var i in eval("new arguments[0]("+Arguments+")")){
if(i!='extend'){
g=eval("new "+arguments[0]+"("+arguments[1]+")."+i)
if(typeof(g)=="undefined"){
eval("this.prototype."+i+"=new
arguments[0]("+arguments[1]+")."+i)
}
else{
eval("this.prototype."+i+"=g")
}
}
}
}
}
//an oval class
function Oval(xaxis,yaxis){
this.xaxis=xaxis
this.yaxis=yaxis
this.classid="oval"
}
//a color class
function Color(color){
this.color=color
}
Color.prototype.RED="#ff0000"
Color.prototype.GREEN="#00ff00"
Color.prototype.BLUE="#0000ff"
//point class - a base class
function Point(x,y){
this.x=x
this.y=y
}
//SUBCLASSES INHERIT APPENDED CLASS METHODS AND PROPERTIES.
Point.prototype.moveto=function(x,y){this.x=x;this.y=y}
Point.prototype.moveby=function(dx,dy){this.x+=dx;this.y+=dy}
//circle class - a subclassing class
//OBJECTS CAN INHERIT FROM MULTIPLE CLASSES.
function Circle(radius,color){
Circle.extend(Oval,[radius,radius]) //inherits from the more
generic oval class
Circle.extend(Color,[color]) //also inherits from the color class
this.classid="circle"
}
//No arguments
function CompanyTag(){
this.company="someco"
this.author="Robret Spoons"
this.date="01.10.04"
}
//mycircleclass subclasses a suclassing class
//FULL OOP CLASS INHERITANCE
function MyCircleclass(radius,color,x,y){
MyCircleclass.extend(Circle,[radius,color]) //Inherits from Circle
MyCircleclass.extend(Point,[x,y]) //Inherits from Point
MyCircleclass.extend(CompanyTag,[null]) //Inherits from
CompanyTag - no arguments
this.classid="mycircleclass"
}
//Simple utility method to itterate through and
//return an objects enumerable methods and properties
function showprops(obj){
var s=""
for(var i in obj)
if(i!='extend')s+=i+": "+eval("obj."+i)+"\n"
return s
}
//CONSTRUCTING THE SUBCLASSED OBJECT AND USING AN INHERITED METHOD
mycircle=new MyCircleclass(13,"blue",100,120)
mycircle.moveby(2,-4)
mycircle.color=mycircle.RED
alert("mycircle\n"+showprops(mycircle)) //shows all enumerable methods
and properties
Thanks
I believe the 'extend' function below will allow you to use full
'class inheritance' in javascript, but I would like to verify it.
The extend function allows the following:
1) inheriting from multiple classes,
2) inheriting an inherited classes inheritances (awkward to say),
3) inheriting appended prototype methods and properties of inhertited
classes.
Thanks.
//----------------------------------------------------------
//rspoons
//'extend' must have two arguments, the object to inherit and
//an Array of arguments to supply the superclass.
//If the superclass requires no arguments, [null] must be passed.
Object.prototype.extend=function(){
if(arguments[1]){ //make sure argument(s) are supplied
for(var i=0;i<arguments[1].length;i++){
if(typeof(arguments[1])=="string")
arguments[1]="'"+arguments[1]+"'"
}
if(arguments[1].length>0){Arguments=arguments[1].join(",")}
else{Arguments=arguments[1][0]}
for(var i in eval("new arguments[0]("+Arguments+")")){
if(i!='extend'){
g=eval("new "+arguments[0]+"("+arguments[1]+")."+i)
if(typeof(g)=="undefined"){
eval("this.prototype."+i+"=new
arguments[0]("+arguments[1]+")."+i)
}
else{
eval("this.prototype."+i+"=g")
}
}
}
}
}
//an oval class
function Oval(xaxis,yaxis){
this.xaxis=xaxis
this.yaxis=yaxis
this.classid="oval"
}
//a color class
function Color(color){
this.color=color
}
Color.prototype.RED="#ff0000"
Color.prototype.GREEN="#00ff00"
Color.prototype.BLUE="#0000ff"
//point class - a base class
function Point(x,y){
this.x=x
this.y=y
}
//SUBCLASSES INHERIT APPENDED CLASS METHODS AND PROPERTIES.
Point.prototype.moveto=function(x,y){this.x=x;this.y=y}
Point.prototype.moveby=function(dx,dy){this.x+=dx;this.y+=dy}
//circle class - a subclassing class
//OBJECTS CAN INHERIT FROM MULTIPLE CLASSES.
function Circle(radius,color){
Circle.extend(Oval,[radius,radius]) //inherits from the more
generic oval class
Circle.extend(Color,[color]) //also inherits from the color class
this.classid="circle"
}
//No arguments
function CompanyTag(){
this.company="someco"
this.author="Robret Spoons"
this.date="01.10.04"
}
//mycircleclass subclasses a suclassing class
//FULL OOP CLASS INHERITANCE
function MyCircleclass(radius,color,x,y){
MyCircleclass.extend(Circle,[radius,color]) //Inherits from Circle
MyCircleclass.extend(Point,[x,y]) //Inherits from Point
MyCircleclass.extend(CompanyTag,[null]) //Inherits from
CompanyTag - no arguments
this.classid="mycircleclass"
}
//Simple utility method to itterate through and
//return an objects enumerable methods and properties
function showprops(obj){
var s=""
for(var i in obj)
if(i!='extend')s+=i+": "+eval("obj."+i)+"\n"
return s
}
//CONSTRUCTING THE SUBCLASSED OBJECT AND USING AN INHERITED METHOD
mycircle=new MyCircleclass(13,"blue",100,120)
mycircle.moveby(2,-4)
mycircle.color=mycircle.RED
alert("mycircle\n"+showprops(mycircle)) //shows all enumerable methods
and properties
Thanks