Inheritance changes

0 views
Skip to first unread message

Trevan Richins

unread,
Oct 11, 2007, 11:14:24 AM10/11/07
to prototy...@googlegroups.com
I just saw that there were some more changes with the inheritance code.
I'm wondering how stable is the code. Meaning, if I start working on a
project and use the current inheritance code right now, what probability
is there that I'll need to rename functions (addMethods) or change
function calls (such as changing the $super construct)?

Thanks,

Trevan

Sam Stephenson

unread,
Oct 11, 2007, 1:50:08 PM10/11/07
to prototy...@googlegroups.com

The changes to the class API introduced in [7833] (http://dev.rubyonrails.org/changeset/7833
) are a result of feedback we've gotten from 1.6.0_rc0, and of
numerous lengthy internal debates. Our goal was to improve the
clarity of the API as well as pave the way for features that may make
their way into future versions of Prototype.

To that end, we've eliminated the Class.extend and Class.mixin
methods, improved the Class.create syntax, and changed the way you add
methods to existing classes. The API is still backwards-compatible
with all pre-1.6 versions of Prototype. We haven't changed the way
inheritance works, and you still access superclass methods using the
special $super argument.

The biggest change is that all classes (as in the objects returned by
Class.create(), not instances of those objects) now have a method
named "addMethods." This method takes a single argument, an object
whose properties become instance methods and properties for all
instances of the class.

For example, in 1.6.0_rc0, where you'd write:

var Foo = Class.create();
Class.extend(Foo, { /* instance methods */ });

We've changed this so you now write:

var Foo = Class.create();
Foo.addMethods({ /* instance methods */ });

As in 1.6.0_rc0, you can still add instance methods directly from
Class.create:

var Foo = Class.create({ /* instance methods */ });

Also unchanged from 1.6.0_rc0, you can inherit from a superclass by
passing the class as the first argument to Class.create (and you can
optionally specify instance methods afterwards):

var Bar = Class.create(Foo);
var Baz = Class.create(Foo, { /* instance methods */ });

Internally, Prototype calls addMethods on the newly created class to
add instance methods passed to Class.create.

Finally, we've modified Class.create to take a variable number of
arguments, so you can now specify multiple collections of instance
methods to add to the class, in order. This is useful for adding
mixins to the class before defining your own methods:

var Bar = Class.create(Foo, Enumerable, SomeMixin, { /* instance
methods */ });
var Baz = Class.create(Enumerable, { /* instance methods */ });

1.6.0_rc1 will be released very soon with these changes, and while we
can't make any guarantees, we've no plans to change the API before the
final release of 1.6.0 (particularly since these changes are being
documented right now in Christophe Porteneuve's upcoming "Prototype &
Scriptaculous" book).

-sam


Mislav Marohnić

unread,
Oct 11, 2007, 6:24:30 PM10/11/07
to prototy...@googlegroups.com
On 10/11/07, Sam Stephenson <s...@37signals.com> wrote:

The changes to the class API introduced in [7833] (http://dev.rubyonrails.org/changeset/7833
) are a result of feedback we've gotten from 1.6.0_rc0, and of
numerous lengthy internal debates.  Our goal was to improve the
clarity of the API as well as pave the way for features that may make
their way into future versions of Prototype.

And let us not forget the nice tutorial at
http://prototypejs.org/learn/class-inheritance

;)
Reply all
Reply to author
Forward
0 new messages