Skip to content

Instantly share code, notes, and snippets.

@k33g
Created November 22, 2010 15:20
Show Gist options
  • Save k33g/710104 to your computer and use it in GitHub Desktop.
Save k33g/710104 to your computer and use it in GitHub Desktop.
inheritance of private members in javascript without prototype
function animal(p){
var note = "a note from animal";
var name = p
this.getNote=function(){return note}
this.setNote=function(value){note = value}
this.getName=function(){return name;}
this.setName=function(value){name=value;}
test=function(){return 'this is a test';}
this.callTest=function(){return test();}
}
function dog(p){
var self = new animal(p);
var specy = "???";
self.getSpecy=function(){return specy}
self.setSpecy=function(value){specy = value}
self.hello = function(){return 'Hello from '+self.getName();}
return self;
}
function puppy(p){
var self = new dog(p);
return self;
}
a = new animal("Bouba");
b = new dog("Rex"); b.setNote("a note from Rex"); b.setSpecy("Berger Belge");
c = new dog("Wolf"); c.setNote("a note from Wolf"); c.setSpecy("Chien Loup");
d = new puppy("Junior"); d.setNote("a note from Junior"); d.setSpecy("Caniche");
e = new puppy("Germaine"); e.setNote("a note from Germaine"); e.setSpecy("York");
console.log(a.getName()+' '+a.getNote());
console.log(d.getName()+' '+d.getNote()+' '+d.getSpecy());
console.log(e.getName()+' '+e.getNote()+' '+e.getSpecy());
console.log(b.getName()+' '+b.getNote()+' '+b.getSpecy());
console.log(c.getName()+' '+c.getNote()+' '+c.getSpecy());
console.log(b.hello());
console.log(c.hello());
console.log(b.hello());
console.log(d.hello());
console.log(e.hello());
console.log(e.callTest());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment