JavaScript Phrasebook

Date.prototype.isLeapYear = isLeapYear;

The prototype property can also be used to extend built-in JavaScript classes. In the following code, a function, isLeapYear(), is implemented that determines whether the return value of getFullYear() is a leap year. Note that the getFullYear() method is not implemented; using the prototype property, however, isLeapYear() becomes a method of the Date object and thus also has access to Date.getFullYear().

Extending the Date Class (extend.html)

<script language="JavaScript" type="text/javascript"> function isLeapYear() { var y = this.getFullYear(); return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)); } Date.prototype.isLeapYear = isLeapYear; var d = new Date(); window.alert(d.isLeapYear()); </script>

JavaScript OOP Enhancements in Microsoft Atlas

The Microsoft AJAX Framework Atlas (http://atlas.asp.net/) also implements several new OOP extensions to JavaScript, making some standard OOP techniques simpler to implement. Among these features are the following:

  • Namespaces

  • Inheritance and access to base methods

  • Abstract classes and methods

  • Interfaces

Especially with the new AJAX hype, more and more libraries emerge that also spice up JavaScript's OOP support. Another option that demonstrates JavaScript OOP well is prototype.js (http://prototype.conio.net/).

Категории