Skip to content

Instantly share code, notes, and snippets.

@k33g
Created November 24, 2010 07:41
Show Gist options
  • Save k33g/713280 to your computer and use it in GitHub Desktop.
Save k33g/713280 to your computer and use it in GitHub Desktop.
Private members inheritance + interfaces + properties
/*--- interfaces definition ---*/
function IBase(){
this.Id = "";
}
function IHuman(){
this.getName=function(){}
this.setName=function(){}
this.talking=function(){}
this.walking=function(){}
}
/*--- Class Human ---*/
/* if no inheritance, "that" notation + "return that" are not mandatory */
function Human(){
$(this).implements([IBase,IHuman]);
this.Id = "not used";
var name = "John DOE";
this.getName=function(){return name;}
this.setName=function(value){name=value;}
this.talking=function(){console.log(name +' is talking ...');}
this.walking=function(){console.log(name+' is walking ...');}
/*Constructor*/
this.ctor = function(p){
if(p!=undefined){name = p;}
console.log('constructor of '+this.getTypeName()+' : '+name);
}
}
/*----- Human -----*/
console.log("----- Human -----");
var h1 = $(Human).new();
h1.talking();
h1.walking();
/*--- Class Man ---*/
function Man(){
var that = $(this).extends(Human);
/*Constructor*/
that.ctor = function(p){
if(p!=undefined){name = p;} //better : that.setName(p)
console.log('constructor of '+that.getTypeName()+' : '+p);
Man.increment();
}
return that;
}
/*--- Static ---*/
Man.counter = 0;
Man.getCounter=function(){return Man.counter;}
Man.increment=function(){Man.counter+=1;}
/*----- Man -----*/
console.log("----- Man -----");
var m1 = $(Man).new("Steve Jobs");
var m2 = $(Man).new("Bill Gates");
m1.setName("Steve JOBS");
m2.setName("Bill GATES");
m1.talking();
m2.talking();
/*call static*/
console.log("----- Static -----");
console.log(Man.counter+' '+Man.getCounter());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment