mustcodemore.com

1.10.2008

Prototypes in Javascript

Extensibility has always been an important charastic of Object Oriented programming. In JavaScript this is accomplished by using the prototype property. Using the prototype property one can extend both JavaScript's intrinsic object classes, as well as your own. Once a class is extended all instances of said class will inherit whatever additional properties (or overridden properties) you create in your prototype.

Its a handy feature, especially for tracking activities. Heres a simple example to extend a click event of an object in the DOM.

SomeObject.prototype.click = function(e) { 
//perform activity tracking here
}

Basically your object "SomeObject" now has a modified click event, which (if you put some code in there) would perform your click tracking on a simple DTHML DOM element. This can include a call to server side scripts to perform a database insert or write to a log file.

Here is a link to a great multi-part article on prototyping in JavaScript :

http://www.webreference.com/js/column34/index.html

0 Comments:

Post a Comment

<< Home