Let me first thank you for helping me differentiate the words
"function" and "method" . I mean really, perhaps 1 year from now, when
someone thank me for the same reason, I wouldn't think of it much, I
would think that the road ahead is still far for that person, which is
how I feel for myself. But that definitely clears my mind a lot for
both function and method seem to DO something.

Now, let me try to make that method of mine and share it here.



On Aug 27, 4:16 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> Yes, JavaScript objects can have method-like things. This statement:
>
>     obj.doSomething();
>
> ...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
> things:
>
> 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:http://blog.niftysnippets.org/2008/03/mythical-methods.html
>
> HTH,
> --
> 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 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to