I do Javascript R & D, and although I read Javascript: the ultimate guide and Javascript object-oriented programming , I still have a minor letting go of my OOP class and object-based lexical OOP.
I like the modules. Namespaces, subclasses, and interfaces. w00t. Here is what I play with:
var Classes = { _proto : { whatAreYou : function(){ return this.name; } }, Globe : function(){ this.name = "Globe" }, Umbrella : new function(){ this.name = "Umbrella" }(), Igloo : function(){ function Igloo(madeOf){ this.name = "Igloo" _material = madeOf; } // Igloo specific Igloo.prototype = { getMaterial : function(){ return _material; } } // the rest for(var p in Classes._proto){ Igloo.prototype[p] = Classes._proto[p] } return new Igloo(arguments[0]); }, House : function(){ function House(){ this.name = "My House" } House.prototype = Classes._proto return new House() } } Classes.Globe.prototype = Classes._proto Classes.Umbrella.prototype = Classes._proto $(document).ready(function(){ var globe, umb, igloo, house; globe = new Classes.Globe(); umb = Classes.Umbrella; igloo = new Classes.Igloo("Ice"); house = new Classes.House(); var objects = [globe, umb, igloo, house] for(var i = 0, len = objects.length; i < len; i++){ var me = objects[i]; if("whatAreYou" in me){ console.log(me.whatAreYou()) }else{ console.warn("unavailable") } } })
I am trying to find the best way to modular code (and understand prototyping) and share everything. The Globe notification is a feature that needs to be created using new , Umbrella is single-point and has already been announced, Igloo uses what I thought at work today and seems to work just as well as it hopes, and House is still one Iglooesque function for testing.
The result of this:
Globe unavailable Igloo My House
So far so good. The Globe prototype must be declared outside the Classes object for syntax reasons, Umbrella cannot accept because of its existing one (or an instance created or ... dunno the "correct" term for this), and Igloo has some closure that declares this for you .
BUT...
If I had to change it to:
var Classes = { _proto : { whatAreYou : function(){ return _name; } }, Globe : function(){ _name = "Globe" }, Umbrella : new function(){ _name = "Umbrella" }(), Igloo : function(){ function Igloo(madeOf){ _name = "Igloo" _material = madeOf; } // Igloo specific Igloo.prototype = { getMaterial : function(){ return _material; } } // the rest for(var p in Classes._proto){ Igloo.prototype[p] = Classes._proto[p] } return new Igloo(arguments[0]); }, House : function(){ function House(){ _name = "My House" } House.prototype = Classes._proto return new House() } } Classes.Globe.prototype = Classes._proto Classes.Umbrella.prototype = Classes._proto $(document).ready(function(){ var globe, umb, igloo, house; globe = new Classes.Globe(); umb = Classes.Umbrella; igloo = new Classes.Igloo("Ice"); house = new Classes.House(); var objects = [globe, umb, igloo, house] for(var i = 0, len = objects.length; i < len; i++){ var me = objects[i]; if("whatAreYou" in me){ console.log(me.whatAreYou()) }else{ console.warn("unavailable") } } })
and make this.name in _name (the "private" property), it does not work and instead outputs:
My House unavailable My House My House
Will anyone be kind enough to explain this? Obviously, _name overwritten at each iteration and does not read the property of the object to which it is bound.
It all seems too complicated if you need this and a kind of weird IMO.
Thanks:)