Yes, JavaScript objects can have method-like things. This statement:


...does two things:

1. It looks up the property 'doSomething' on the object 'obj'.

2. If the value is a function, it calls the function and sets its
context (the `this` value inside the function) to 'obj'. (If the value
isn't a function, it throws an error.)

Taken together, these two things make it seem like JavaScript has
methods.  (It doesn't, but its functions are so powerful that they can
do what methods do -- and plenty more besides.)

So that means that to do what you're talking about, you must do two

1. Make doSomething a property of obj.
2. Have doSomething work with `this` rather than with an argument
passed in.

More about JavaScript's mythical methods in my incredibly anemic
programming blog:

T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Aug 26, 8:31 pm, Nik <niks...@gmail.com> wrote:
> Hello, say I have this function doSomething();
> I have always been passing it an obj (prototype object) to return
> something I want from the obj.
> so it goes like this: doSomething(obj);
> can I do it more like Rails, that is to say, like:
> obj.doSomething()?
> For instance,
> function pulse(obj){
>   Effect.pulsate(obj, {pulses:4, duration:0.5});
> }
> I normall do :
> obj = $('some_div');
> pulse(obj);
> How can I do it like:
> obj.pulse()?
> Thanks!
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-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to