Object Oriented Javascript Tutorial
October 5, 2006 at 1:42 pm | In gyaan | Leave a CommentThis is a simple OOjs tutorial just to get you started on Object Oriented Javascript.
A simple class definition
var exampleObj = {
counter: 0, //attributes
foo: function(){ //method 1
alert("foo");
this.counter++;
},
bar: function(){ //method 2
alert("bar");
this.counter++;
}
to instantiate an object of this above declared type use the defualt java like statement
var obj1= new xampleObj() ;
constructor basically involves variables that are passed as parameters to the new and then initialized to memeber variables inside the class definition
var exampleObject(name){
this.name=name;
}
another important feature is the prototype method . Prototype is a kind of late binding to an already exsiting object. methods can be added to exisitng classes outside the class body by using the prototype method.
This is useful for inheritance and also to extend functionality to exisiting DOM nodes and other defualt javascript objects like image, arrays etc.
exampleObj.prototype.changeName = function(name) {
this.name = name;
}
Inheritance basically involves making an object of type parent in the child . the example below must help
function parentClass() {
this.parenttest = function parentTest() {
return "parentTest";
}
}
function subClass() {
this.parentFrom = parentClass;
this.parentFrom();
this.subtest = function subTest() {
return "childTest";
}
}
var newClass = new subClass();
alert(newClass.subtest()); // yields "childTest"
alert(newClass.parenttest()); // yields "parentTest"
the rest, later.
found this great article about OOjs with reference to C# also a great read
No Comments Yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

