On Class#create

0 views
Skip to first unread message

joneff

unread,
Nov 10, 2009, 7:26:36 PM11/10/09
to Prototype: Core
I was wondering for the longest time why doesn't Class#create have
arguments?

I mean it's obviously to allow inheritance from multiple objects, but
honestly, how often does this happen?

Soooo.. If arguments are provided, Class#create could look like

function create(parent, properties) {
if (typeof parent !== "function") {
properties = parent;
parent = null;
}

... // more code

if (parent) {
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}

if (properties)
klass.addMethods(properties);

... // more code
}

which avoids few function calls. More over, if indeed multiple
inheritance is required it could be done in the addMethods method with
looking for special key (say implements or inheritsFrim or what ever).

However, typeof is kinda unfriendly looking to me. I don't know how
many of you are using MS Client JS, but they have an extra property
__typeName to indicate the type of object being worked with.

So if there was indeed such a mechanism in Prototype, the syntax would
become

if (!parent.__typeName || parent.__typeName != "class")

It is indeed longer than the first one, but looks friendlier (to me)
and actually having an easy way to check whether or not an object is a
class seems handy (again to me).

Any thoughts?

T.J. Crowder

unread,
Nov 11, 2009, 5:44:51 AM11/11/09
to Prototype: Core
Hi,

> I was wondering for the longest time why doesn't Class#create have
> arguments?

Class.create does have arguments, details in the docs[1]. The
implementation doesn't use any _named_ arguments, it accesses its
arguments via the `arguments` array (on the first line of the
function, in fact).

[1] http://api.prototypejs.org/language/class.html#create-class_method

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

joneff

unread,
Nov 11, 2009, 7:29:24 AM11/11/09
to Prototype: Core
I have indeed noticed that. I quite often use function without named
arguments and then populate what I need from the arguments collection.

I was wondering why was that so. I mean creating a class does not need
infinite number of arguments, now would it? In the longest case
scenario it needs class name (prototype creates differently but
still), parent class, own fields / method, an array of mixins to
borrow methods from.

So in Prototype's case, that would be three arguments.

Creating and instantiating a prototype class is notoriously slow. I
was looking for ways (from my point of view) how the process could be
a bit faster. And I say a bit, since notoriously slow means some ticks
slower.

T.J. Crowder

unread,
Nov 11, 2009, 9:36:53 AM11/11/09
to Prototype: Core
Hi,

You can have as many mix-ins as you like, which is why the number of
arguments is variable:

"...any arguments passed are treated as objects, and their methods are
copied over ("mixed in") as instance methods of the new class. In
cases of method name overlap, later arguments take precedence over
earlier arguments."[1]

> Creating and instantiating a prototype class is notoriously slow.

I hadn't realized that using `arguments` (as opposed to
`arguments.callee`) was slow, but a quick experiment shows that you do
indeed pay a performance penalty for it on some major browsers (not
typically as bad as for `arguments.callee`, but pretty bad). Very
interesting, thanks for that. That would indeed hit both Class.create
and initialization, since initialization requires that the Prototype-
generated constructor hands `arguments` to the initializer.

I think the larger issue, though, with defining (creating) classes is
because of Prototype's support for supercalls, which requires that it
decompile every single function in the class (and all mixins) and
perform regexes on each of the resulting strings -- which is not just
a speed issue, but a standards issue as well. I've proposed a solution
for it[2].

The same applies to instantiating: if you use a supercall in your
initializer (which is quite common), you'll pay a big speed penalty
for that -- each method call involving a supercall is very slow
indeed, Prototype actually creates a new function on the fly for the
call, every time(!). My proposal fixes that as well.

[1] http://api.prototypejs.org/language/class.html#create-class_method
[2] http://groups.google.com/group/prototype-core/browse_thread/thread/db9ccdaae4f7f705#

Happy coding,

-- T.J.
Reply all
Reply to author
Forward
0 new messages