Javascript OOP using Literal Notation
From Wiki2
Revision as of 17:57, 27 March 2013 by Tim (talk | contribs) (→javascript OOP using Literal Notation)
javascript OOP using Literal Notation
Literal Notation seems much more readable to me.
OOP single
In cases where you don't need multiple instances of a Class, then it is easy to create a object like this: <syntaxhighlight>
var obj={ tisel : new Array(), tesel : "" , ulsel : '.prog-tes ul', lisel : '.prog-tes ul li', idx : 0, arr : new Array(), new : function(){ this.tesel="dog"; this.tisel[0]="cat"; return "frog"; } }; obj.new();
</syntaxhighlight> You declare all your variables. Make a fake new or constructor to initalize.
OOP multiple instances
If you need multiple instances of a class (many objects) and you want to use literal notation then you could do it like this <syntaxhighlight>
anima=new Object(); anima.large = "elephant" anima['small'] = "mouse"; feline=new Object(); feline=new Array(); feline[4]=anima; function MyClass(animal,feline){ this.tesel=animal; this.tisel=feline; return "frog"; } MyClass.prototype={ ulsel : '.prog-tes ul', lisel : '.prog-tes ul li', idx : 0, arr : new Array(), new : function(){ console.log(this.tesel); this.tesel="jerk"; this.tisel[17]="drugs"; this.tisel[0]="rock"; } }; obj=new MyClass(anima,feline);
</syntaxhighlight> The stuff above function Myclass is just junk to try to see the diffferences between arrays and objects.