Created
November 23, 2010 05:31
-
-
Save k33g/711316 to your computer and use it in GitHub Desktop.
Pattern 2 ;-) private members inheritance or prototype is evil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (){ | |
/*mummy*/ | |
var mum = { | |
select:function(child){ | |
var thatChild = child; | |
return { | |
extends : function(parent){ | |
var instance = new parent; | |
instance.constructor = thatChild.constructor; | |
instance.getTypeName = function(){return instance.constructor.name;} | |
instance.parent = new parent; | |
return instance; | |
}, | |
new : function(){ | |
var instance = new thatChild; | |
instance.getTypeName = function(){return instance.constructor.name;} | |
instance['ctor'].apply(instance,arguments); | |
return instance; | |
} | |
} | |
}/*end of select*/ | |
} | |
if(!window.$){window.$=mum.select;} | |
})(); | |
function animal(p){ | |
var that = this;//pas obligé | |
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 --- */ | |
var test=function(){return 'this is a test from '+name;} | |
/* --- call private --- */ | |
that.callTest=function(){return test();} | |
that.ctor = function(p){ | |
name = p; | |
note = "a note from animal"; | |
} | |
return that;//pas obligé | |
} | |
function dog(){ | |
var that = $(this).extends(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 that = $(this).extends(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 = $(animal).new("BOUBA"); | |
var b = $(dog).new("Rex"); | |
var c = $(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 = $(puppy).new("Junior"); d.setNote("a note from Junior"); d.setSpecy("Caniche"); | |
var e = $(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
line 49 : add "var" :
var test=function(){return 'this is a test from '+name;}