Can I change what new [someClass] will return?

2 views
Skip to first unread message

Luke

unread,
Dec 16, 2010, 11:36:02 AM12/16/10
to Prototype & script.aculo.us
Hi,

if you define a Class like

----------------------------------
var TestClass = Class.create({
initialize: function(element) {
element = $(element);
this.doFunkyStuffWithElement(element)
return element;
},
doFunkyStuffWithElement: function(element) { log("funky stuff
here") }
});
----------------------------------

and you instantiate that Class

var myObject = new TestClass();

is there a way I can change it so (while namespacing my Class-Code so
Prototype's Class will remain untouched) that instantiation of that
class will return something different than what Prototype returns (in
my case a DOM-Object that has been extended with custom Methods)?


---
Why I want to do this: I'm working on a site where you can create
really simple webpages by adding, editing, and removing elements on
your page. To structure my code I make use of prototype's dom-
extending nature and its way of class-inheritance:
There is a base class for elements on the page which has methods for
editing and removing that object and so on. I subclass this base-class
for specific elements where I implement the specific editing/removing/
whatever-code. Now to map my functionality to the elements on the
page, I extend the DOM-Elements which you can edit/etc with the
methods and properties of the class, leaving out unnecessary
constructors etc.

But it is always a little annoying to extend an object with a class,
and then fetch that object again to work with it:

----------------------------------
new EditablePicture($('the_element_I_extend'));
var my_element = $('the_element_I_extend');
my_element.do_something();
----------------------------------

it would be nice to be able to do something like this:

----------------------------------
var my_element = new EditablePicture($('the_element_I_extend'));
my_element.do_something();
----------------------------------

Is that kind of stuff even possible?

Thank you
Lukas

Richard Quadling

unread,
Dec 16, 2010, 12:04:59 PM12/16/10
to prototype-s...@googlegroups.com
> --
> 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 prototype-s...@googlegroups.com.
> To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
>
>

Yep - I think so. Look at around line 80 of prototype.js

function klass() {
return this.initialize.apply(this, arguments);
}

The return is something I've been using since May 2008 with all the
releases and updates since then.

I've not had any issues with my code or with scripty with it.


--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

Luke

unread,
Dec 17, 2010, 3:32:59 AM12/17/10
to Prototype & script.aculo.us
Hi Richard,

thanks for your reply. With

> The return is something I've been using since May 2008 with all the
> releases and updates since then.

Do you mean you have used the native prototype-return, or did you
alter it, and it still worked?

On Dec 16, 6:04 pm, Richard Quadling <rquadl...@gmail.com> wrote:
> > For more options, visit this group athttp://groups.google.com/group/prototype-scriptaculous?hl=en.

Richard Quadling

unread,
Dec 17, 2010, 5:08:48 AM12/17/10
to prototype-s...@googlegroups.com

I build my own prototype. I have a patch which is applied to after the
compiled from source.

So, any class I create, I can return something from the initializer.

I'm trying to find the original request for this feature, but I can't.
It makes my classes work like a Factory if the initializer returns
something other than this.

Luke

unread,
Dec 17, 2010, 5:14:49 AM12/17/10
to Prototype & script.aculo.us
Sounds good. Can you post a link to your source (if you want to share
it and have it on github or something)?

On Dec 17, 11:08 am, Richard Quadling <rquadl...@gmail.com> wrote:
> > For more options, visit this group athttp://groups.google.com/group/prototype-scriptaculous?hl=en.

Richard Quadling

unread,
Dec 17, 2010, 5:15:49 AM12/17/10
to prototype-s...@googlegroups.com

Simply adding "return" to the call to the this.initialize.appy(this,
arguments); should be enough.

Having to do this for each release is a bit of a pain, and I can't see
why this isn't supported as standard.

Richard Quadling

unread,
Dec 17, 2010, 5:17:42 AM12/17/10
to prototype-s...@googlegroups.com
On 17 December 2010 10:14, Luke <kickin...@gmail.com> wrote:
> Sounds good. Can you post a link to your source (if you want to share
> it and have it on github or something)?

I don't use github. I simply build prototype and then add my patch.

I don't have any unit tests for this either, but my users are using it
all day every day (24 hour call centre) for several years.

Luke

unread,
Dec 17, 2010, 7:51:59 AM12/17/10
to Prototype & script.aculo.us
Ok, that works great. Thank you Richard!

On Dec 17, 11:17 am, Richard Quadling <rquadl...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages