Rob Kinyon - Object.extend-0.02
NAME
Object.extend
DESCRIPTION
This provides a new method to the core class Object. The method, called extend(), is used to provide one way to implement inheritance.
DEPENDENCIES
This requires JSAN and Upgrade to be installed.
CORE CLASS EXTENSIONS
These are extensions to core classes provided by JavaScript.
Object.extend( object )
The extend()
method is added to the builtin Object class,
providing attribute inheritan ce.
It is used as so (example from Ajax.Request):
Ajax.Request.prototype = (new Ajax.Base()).extend({
initialize: function( ... ) {
}
...
});
This will provide Ajax.Request with all the methods and attributes found in Ajax.Base.
CAVEATS
Modifying the prototype of core Javascript classes should be avoided, if possible. By doing this, you are modifying ALL objects of this class, regardless of when they were instantiated or by whom, For some classes, such as Number and String, this includes primitives. This can lead to surprising effects and action-at-a-distance.
You have been warned.
SUPPORT
Currently, there is no mailing list or IRC channel. Please send bug reports and patches to the author.
AUTHOR
Rob Kinyon (rob.kinyon@iinteractive.com)
Originally written by Sam Stephenson (sam@conio.net)
My time is generously donated by Infinity Interactive, Inc. http://www.iinteractive.com
/* =head1 NAME Object.extend =head1 DESCRIPTION This provides a new method to the core class Object. The method, called extend(), is used to provide one way to implement inheritance. =head1 DEPENDENCIES This requires JSAN and Upgrade to be installed. =cut */ try { JSAN.use( 'Upgrade.Function.apply' ); } catch (e) { throw "Object.extend requires JSAN to be loaded"; } /* =head1 CORE CLASS EXTENSIONS These are extensions to core classes provided by JavaScript. =cut */ /* =head2 Object.extend( object ) The C<extend()> method is added to the builtin Object class, providing attribute inheritan ce. It is used as so (example from Ajax.Request): Ajax.Request.prototype = (new Ajax.Base()).extend({ initialize: function( ... ) { } ... }); This will provide Ajax.Request with all the methods and attributes found in Ajax.Base. =cut */ if ( ! Object.extend ) { Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; } } if ( ! Object.prototype.extend ) { Object.prototype.extend = function(object) { return Object.extend.apply(this, [this, object]); } } /* =head1 CAVEATS Modifying the prototype of core Javascript classes should be avoided, if possible. By doing this, you are modifying B<ALL> objects of this class, regardless of when they were instantiated or by whom, For some classes, such as Number and String, this includes primitives. This can lead to surprising effects and action-at-a-distance. You have been warned. =head1 SUPPORT Currently, there is no mailing list or IRC channel. Please send bug reports and patches to the author. =head1 AUTHOR Rob Kinyon (rob.kinyon@iinteractive.com) Originally written by Sam Stephenson (sam@conio.net) My time is generously donated by Infinity Interactive, Inc. L<http://www.iinteractive.com> =cut */