Hi,

> Ah okay, so you can use "Parent.prototype.method.call()" as an alternative
> for the $super argument?

Yes. The first argument to `call` is the value to use as `this` during
the call (so if you're calling it from within a subclass, you'd supply
`this`). Subsequent arguments, if any, are passed on to the function
as its argments (the `x` in my example was there to demonstrate that).
You can also use `apply` instead of call; the only difference is that
instead of giving the arguments to pass to the function as discrete
arguments after the `this` arg, you supply an array. So all three of
the below have exactly the same effect:

1. Using `call` with discrete args:
Parent.prototype.call(this, a, b, c);

2. Using `apply` with a literal array:
Parent.prototype.apply(this, [a, b, c]);

3. Using `apply` with a non-literal array:
var args = [];
args.push(a);
args.push(b);
args.push(c);
Parent.prototype.apply(this, args);

> Would that be better to use for performance critical apps instead of the
> $super argument?

When Prototype sets up the `$super` argument for your subclass
function, to do its magic it has to create four additional functions
for each of your subclass functions that declares `$super` (this is
done during `Class.create` / `Class#addMethods`). When code calls your
subclass function, there's an extra function call involved (the
calling code actually calls a  wrapper that turns around and calls
your function). When you call `$super`, you're actually calling
another wrapper that then calls the ancestor function.

So overall, you save the creation of four functions per subclass
function at `Class.create` / `Class#addMethods` time, and the overhead
of two extra function calls at runtime. Whether it matters will depend
a LOT on the application. Most of the time, it probably doesn't matter
(the overhead will typically be swamped by the work your functions are
actually doing). Using the parent directly, you also aren't exposed to
issues with `Function#toString` not working on all platforms
(`Class.create` / `Class#addMethods` relies on `Function#toString1).

The downside is, of course, that
`Parent.prototype.method.call(this, ...)` is a bit awkward and you end
up repeating the ancestor name a *lot* in your code. If you rebase
your subclass (perhaps inserting a layer of abstraction), you have to
do a lot of search and replace. You can avoid that, because Prototype
sets up a `superclass` property for you on your class object
(`Child.superclass = Parent`), so you can use
`Child.superclass.prototype.method.call(this, ...)` instead of
`Parent.prototype.method.call(this, ...)`, but that's even more long-
winded. All in all, this is why I prefer the mechanism described in my
blog post -- it's efficient (no extra function calls, no extra
functions), but easy to use. With my mechanism, the subclass calls the
ancestor's version like this: `method.$super.call(this, ...)`. Not as
short as `$super(...)`, but a lot shorter than
`Child.superclass.prototype.method.call(this, ...)`. :-)

FWIW & HTH,

-- T.J. :-)

On Nov 30, 8:05 am, Johan Arensman <[email protected]> wrote:
> Ah okay, so you can use "Parent.prototype.method.call()" as an alternative
> for the $super argument?
>
> Would that be better to use for performance critical apps instead of the
> $super argument?
>
> On Mon, Nov 29, 2010 at 8:03 PM, T.J. Crowder <[email protected]>wrote:
>
>
>
>
>
>
>
> > Hi,
>
> > From within a call to `SectionText`'s `extension` function, you can
> > use `$super` to call `Section`'s version, although be aware that
> > Prototype's `$super` support relies on some ugly things under the
> > covers (like function decompilation).
>
> > There's a clunky alternative which you can use anywhere (including
> > within `SectionText`'s `extension` function:
> > `Section.prototype.extension.call(instanceReference, ...)`. Here's an
> > example:
> >http://jsbin.com/iquto5
>
> > Or you can go a totally different direction:
> >http://blog.niftysnippets.org/2009/09/simple-efficient-supercalls-in....
>
> > Happy coding,
> > --
> > T.J. Crowder
> > Independent Software Engineer
> > tj / crowder software / com
> > www / crowder software / com
>
> > On Nov 29, 4:45 pm, Luke <[email protected]> wrote:
> > > Thanks. Fermion in the Prototype-IRC also told me the same thing. But
> > > he said you cannot access the parent's property. It will be
> > > overwritten by the child. Is that true or do you know any way to
> > > access it from the child? $super just seems to contain the parent's
> > > method that has the same name like the child's method it is passed
> > > to... .
>
> > > On Nov 29, 1:23 pm, Johan Arensman <[email protected]> wrote:
>
> > > > You can use the $super parameter name to access the property of the
> > parent
> > > > class.
>
> > > > See:http://api.prototypejs.org/language/Class/create/
>
> > > > On Mon, Nov 29, 2010 at 12:15 PM, Luke <[email protected]> wrote:
> > > > > Hi there!
>
> > > > > Is there a way you can inherit from a class and extend a property
> > from
> > > > > the parent class with the same property of the subclass? Like if I
> > > > > have the following 2 classes:
>
> > > > > === ===
>
> > > > > var Section = Class.create({
>
> > > > >  attach: function(element) {
> > > > >    Object.extend(element, this.extension);
> > > > >  }
>
> > > > >  extension: {
> > > > >    // Methods and properties here
> > > > >  }
> > > > > }
>
> > > > > === and ===
>
> > > > > var SectionText = Class.create(Section, {
>
> > > > >  extension: {
> > > > >    // More methods and properties here
> > > > >  }
>
> > > > > });
>
> > > > > === ===
>
> > > > > is somehow possible to extend the extension-property of the parent
> > > > > class with the extension-property of the subclass rather than
> > > > > replacing it? Can I maybe somehow access the parent's property from
> > > > > the subclass to manually extend it?
>
> > > > > Thanks,
> > > > > Luke
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > Groups
> > > > > "Prototype & script.aculo.us" group.
> > > > > To post to this group, send email to
> > > > > [email protected].
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected]<prototype-scriptaculou
> > > > >  s%[email protected]><prototype-scriptaculou
> > s%[email protected] <s%[email protected]>>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/prototype-scriptaculous?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Prototype & script.aculo.us" group.
> > To post to this group, send email to
> > [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<prototype-scriptaculou 
> > s%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/prototype-scriptaculous?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to