Skip to content

Instantly share code, notes, and snippets.

@k33g
Created November 23, 2010 04:18
Show Gist options
  • Save k33g/711237 to your computer and use it in GitHub Desktop.
Save k33g/711237 to your computer and use it in GitHub Desktop.
Pattern ;-) private members inheritance or prototype is evil
(function (){
/*mummy*/
var mum = {
extends:function(child){
var thatChild = child;
return new function(){
this.with = function(parent){
var instance = new parent;
instance.constructor = thatChild.constructor;
instance.getTypeName = function(){return instance.constructor.name;}
instance.parent = new parent;
return instance;
}
}
},/*end of extends*/
select:function(p_class){
var instance = new p_class;
console.log(instance);
return new function(){
this.new = function(){
instance.getTypeName = function(){return instance.constructor.name;}
instance['ctor'].apply(instance,arguments);
return instance;
}
}
}/*end of select*/
}/*END of var mum*/
if(!window.$extends){window.$extends=mum.extends;}
if(!window.$select){window.$select=mum.select;}
})();
function animal(p){
var that = this;
var note;
var name;
that.getNote=function(){return note}
that.setNote=function(value){note = value}
that.getName=function(){return name;}
that.setName=function(value){name=value;}
/* --- private --- */
test=function(){return 'this is a test';}
/* --- call private --- */
that.callTest=function(){return test();}
that.ctor = function(p){
name = p;
note = "a note from animal";
}
return that;
}
function dog(){
//var that = this.extends(animal);
var that = $extends(this).with(animal);
/*--- Private Members ---*/
var specy = "???";
/*--- Public Getters & Setters ---*/
that.getSpecy=function(){return specy}
that.setSpecy=function(value){specy = value}
/*--- Public Method ---*/
that.hello = function(){return 'Hello from '+that.getName();}
/*--- Constructor ---*/
that.ctor = function(p){
that.setName(p)
console.log("*** call dog constructor");
}
return that;
}
function puppy(){
//var self = this.extends(dog);
var that = $extends(this).with(dog);
/* parent ctor is called if no ctor*/
/*that.ctor = function(p){
console.log("*** ctor of puppy is called ****");
that.setName(p)
}*/
return that;
}
var a = $select(animal).new("BOUBA");
var b = $select(dog).new("Rex");
var c = $select(dog).new("Wolf");
b.setNote("a note from Rex");
b.setSpecy("Berger Belge");
c.setNote("a note from Wolf"); c.setSpecy("Chien Loup");
var d = $select(puppy).new("Junior"); d.setNote("a note from Junior"); d.setSpecy("Caniche");
var e = $select(puppy).new("Germaine"); e.setNote("a note from Germaine"); e.setSpecy("York");
console.log(a.getName()+' '+a.getNote()+' '+a.getTypeName());
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()+' '+b.getTypeName());
console.log(c.hello()+' '+c.getTypeName());
console.log(b.hello()+' '+b.getTypeName());
console.log(d.hello()+' '+d.getTypeName()+' '+d.parent.getTypeName());
console.log(e.hello()+' '+e.getTypeName());
console.log(e.callTest());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment