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.prototyp e.extend=functi on(){
if(arguments[1]){ //make sure argument(s) are supplied
for(var i=0;i<arguments[1].length;i++){
if(typeof(argum ents[1][i])=="string")
arguments[1][i]="'"+argumen ts[1][i]+"'"
}
if(arguments[1].length>0){Argu ments=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]+"("+argumen ts[1]+")."+i)
if(typeof(g)==" undefined"){
eval("this.prot otype."+i+"=new
arguments[0]("+arguments[1]+")."+i)
}
else{
eval("this.prot otype."+i+"=g")
}
}
}
}
}
//an oval class
function Oval(xaxis,yaxi s){
this.xaxis=xaxi s
this.yaxis=yaxi s
this.classid="o val"
}
//a color class
function Color(color){
this.color=colo r
}
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=functio n(x,y){this.x=x ;this.y=y}
Point.prototype .moveby=functio n(dx,dy){this.x +=dx;this.y+=dy }
//circle class - a subclassing class
//OBJECTS CAN INHERIT FROM MULTIPLE CLASSES.
function Circle(radius,c olor){
Circle.extend(O val,[radius,radius]) //inherits from the more
generic oval class
Circle.extend(C olor,[color]) //also inherits from the color class
this.classid="c ircle"
}
//No arguments
function CompanyTag(){
this.company="s omeco"
this.author="Ro bret Spoons"
this.date="01.1 0.04"
}
//mycircleclass subclasses a suclassing class
//FULL OOP CLASS INHERITANCE
function MyCircleclass(r adius,color,x,y ){
MyCircleclass.e xtend(Circle,[radius,color]) //Inherits from Circle
MyCircleclass.e xtend(Point,[x,y]) //Inherits from Point
MyCircleclass.e xtend(CompanyTa g,[null]) //Inherits from
CompanyTag - no arguments
this.classid="m ycircleclass"
}
//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(1 3,"blue",100,12 0)
mycircle.moveby (2,-4)
mycircle.color= mycircle.RED
alert("mycircle \n"+showprops(m ycircle)) //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.prototyp e.extend=functi on(){
if(arguments[1]){ //make sure argument(s) are supplied
for(var i=0;i<arguments[1].length;i++){
if(typeof(argum ents[1][i])=="string")
arguments[1][i]="'"+argumen ts[1][i]+"'"
}
if(arguments[1].length>0){Argu ments=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]+"("+argumen ts[1]+")."+i)
if(typeof(g)==" undefined"){
eval("this.prot otype."+i+"=new
arguments[0]("+arguments[1]+")."+i)
}
else{
eval("this.prot otype."+i+"=g")
}
}
}
}
}
//an oval class
function Oval(xaxis,yaxi s){
this.xaxis=xaxi s
this.yaxis=yaxi s
this.classid="o val"
}
//a color class
function Color(color){
this.color=colo r
}
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=functio n(x,y){this.x=x ;this.y=y}
Point.prototype .moveby=functio n(dx,dy){this.x +=dx;this.y+=dy }
//circle class - a subclassing class
//OBJECTS CAN INHERIT FROM MULTIPLE CLASSES.
function Circle(radius,c olor){
Circle.extend(O val,[radius,radius]) //inherits from the more
generic oval class
Circle.extend(C olor,[color]) //also inherits from the color class
this.classid="c ircle"
}
//No arguments
function CompanyTag(){
this.company="s omeco"
this.author="Ro bret Spoons"
this.date="01.1 0.04"
}
//mycircleclass subclasses a suclassing class
//FULL OOP CLASS INHERITANCE
function MyCircleclass(r adius,color,x,y ){
MyCircleclass.e xtend(Circle,[radius,color]) //Inherits from Circle
MyCircleclass.e xtend(Point,[x,y]) //Inherits from Point
MyCircleclass.e xtend(CompanyTa g,[null]) //Inherits from
CompanyTag - no arguments
this.classid="m ycircleclass"
}
//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(1 3,"blue",100,12 0)
mycircle.moveby (2,-4)
mycircle.color= mycircle.RED
alert("mycircle \n"+showprops(m ycircle)) //shows all enumerable methods
and properties
Thanks
Comment