Difference between revisions of "Javascript OOP using Literal Notation"
From Wiki2
(Created page with "===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, th...") |
(No difference)
|
Revision as of 16:54, 27 March 2013
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[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>