Thursday, 17 July 2008

Javascript prototypes

« Firefox extensions | Main | org-mode for emacs »

As I continue with my firefox extension writing, I wanted to refresh my knowledge of Javascript prototypes. Unfortunately most of the examples that I found were not very good. The normally very good site http://www.w3schools.com/ has an example that doesn't really demonstrate the feature (the result is the same without the prototype line); it also does not have anything to do with math.

The following modification to the w3schools example might be more illustrative:

function employee(name,jobtitle,born)
{
  this.name=name;
  this.jobtitle=jobtitle;
  this.born=born;
}

var fred=new employee("Fred Flintstone","Caveman",1970);
var barney=new employee("Barney Rubble","Caveman",1971);

employee.prototype.salary=19000;
fred.salary=20000;

print("Fred's salary is "+fred.salary);
print("Barney's salary is "+barney.salary);
Posted by james at 11:50 AM in Tools and Programming

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« July »
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
2728293031