GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Thursday, December 16, 2010

Advance Javascript - Class Based Prototype

The two kinds of class definition in javascript: As is and Prototype based.
In prototype based, you declare the function as base class.

>> function Data(id,name){
>>   this.id = id;
>>   this.name = name;
>> }

Then you make data and function as prototype:

>> Data.prototype.city = "denpasar";
>> Data.prototype.setId = function(id){
>>   this.id = id;
>> }
>> Data.prototype.setName = function(name){
>>   this.name = name;
>> }
>> Data.prototype.setCity = function(city){
>>   this.city = city;
>> }
>> Data.prototype.getId = function(){
>>   return this.id;
>> }
>> Data.prototype.getName = function(){
>>   return this.name;
>> }
>> Data.prototype.getCity = function(){
>>   return this.city; 
>> }

After definition like above, you can use this below to use it:
>> var lady = new Lady(3,"lady gaga");
>> lady.setCity("New York");


Share/Bookmark

No comments:

Post a Comment