Skip to content

Instantly share code, notes, and snippets.

@k33g
Created November 22, 2010 13:38
Show Gist options
  • Save k33g/709982 to your computer and use it in GitHub Desktop.
Save k33g/709982 to your computer and use it in GitHub Desktop.
oo javascript again
$class({
//Class definition
$def:function Animal(){
this.name = "";
this.getName=function(){return this.name;}
this.setName=function(value){this.name=value;}
this.Animal=function(p_name){
this.name = p_name;
}
}
});
var animal = $get($.Animal).new("Bouba");
console.log(animal.getName());
$class({
//Inheritance
$ext:$.Animal,
//Class definition
$def:function Cat(){
this.meow=function(){
return 'meoowww, i am '+this.getName()+' the '+this.getTypeName();
}
this.Cat=function(p_name){
this.Animal(p_name);
}
}
});
var felix = $get($.Cat).new("Felix");
console.log(felix.meow());
console.log(felix.getTypeName()+' extends '+felix.parent.getTypeName());
var sylvestre = $get($.Cat).new("Sylvestre");
//Interfaces definition
$interface(
function ICarnivorous(){
this.eatMeat = function(){};
}
);
$interface(
function IMammal(){
this.parasagittalMember = function(){};
}
);
$class({
//Inheritance
$ext:$.Animal,
//Implements
$imp:[$.ICarnivorous,$.IMammal],
//Class definition
$def:function Dog(){
this.eatMeat = function(){
return this.getName()+' loves meat because it is a '+this.getTypeName();
}
this.parasagittalMember = function(){return true;}
this.Dog = function(p_name){
this.Animal(p_name);
}
}
});
var lassie = $get($.Dog).new("Lassie");
console.log(lassie.eatMeat());
console.log(lassie.parasagittalMember());
console.log(lassie.parent);
console.log($.Dog.parent);
console.log(lassie.interfaces);
console.log($.Dog.interfaces);
console.log($.Dog.interfaces[0]);
console.log($.Dog.interfaces[1]);
console.log(lassie.getName());
$class({
$ext:$.Dog,
$def:function Puppy(){
this.Puppy = function(p_name){
this.Dog(p_name);
}
},
//Static methods
$sta:{
getInstance:function(){return $get($.Puppy).new();},
getInstanceWithParam:function(p){return $get($.Puppy).new(p);},
hello:function(){return 'hello';}
}
});
var junior = $get($.Puppy).new();
console.log(junior.parent.getTypeName());//Dog
junior.setName("Junior");
//junior.hello();
var soeurette = $.Puppy.getInstanceWithParam("Princesse");
console.log(soeurette.eatMeat());
console.log(junior.eatMeat());
console.log(lassie.getName());
console.log(junior.getTypeName()+
' extends '+junior.parent.getTypeName() +
' which extends '+junior.parent.parent.getTypeName());
console.log($.Puppy.getTypeName());
console.log($.Puppy.parent.getTypeName());
console.log($.Puppy.parent.parent.getTypeName());
console.log(animal.getName());
console.log(felix.getName());
console.log(sylvestre.getName());
console.log(lassie.getName());
console.log(junior.getName());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment