Yes, of course, you are right! Oh, God, I just don’t believe this.
It’s because it was so long ago I wrote procedural code that I would
never have thought someone would ever want to use it with a library.
After all people (should) learn proper object oriented programming
practices first before they would start to use libraries..
In object oriented style functions are grouped together with the data
in objects that they will work on as methods and usually the context
object is adjusted if necessary to give a different meaning to the
‘this’ keyword inside them. This is why I would never ever try to pass
an object as an argument to a function if I wanted to make it a method
of that object. Awkward!
It’s like using a USB to PS2 adapter. You give up a large part of
higher functionality and flexibility to serve the needs of something
much worse.
In the correct way ‘methodize’ would be an alternative of ‘bind’, with
the exception of not explicitly passing the object in the closure like
in ‘bind’ to be preserved and used as a new context object of the
function, but passing a newly created static method back to become a
method of the object and gain access to it through the ‘this’ keyword,
as I described.
In this way proper object oriented code could exploit the this context
object inside the function and old procedural code could also be
adapted with something as simple as this:
Outdated_function(param:2, param:3){
obj_param_1 = this;
/*
Some code I don’t want to rewrite that uses the original passed-in
obj_param_1.
*/
}
On Apr 16, 10:20 pm, petrob <[email protected]> wrote:
> http://prototypejs.org/api/function/methodize
>
> On Apr 16, 10:14 pm, petrob <[email protected]> wrote:
>
>
>
>
>
>
>
> > The whole point of using the methodize Function:prototype method is
> > NOT to have to pass an object in any function as a second argument,
> > but to call the function in the scope of any arbitrary object, isn't
> > it? At least, this is what the documentation explains.
>
> > On Apr 16, 9:49 am, Victor <[email protected]> wrote:
>
> > > Where is the second argument in multiplier()? I'll try to explain in
> > > example:
>
> > > function multiplier(obj, times) {
> > > times = isNaN(times) ? 2 : times;
> > > if (typeof obj.value != 'undefined') {
> > > return obj.value * times;
> > > } else {
> > > alert(obj + ': does not have a value property!');
> > > }
>
> > > }
>
> > > obj_a.multiply = multiplier.methodize();
> > > multiplier(obj_a, 2); // same as obj_a.multiply(2);
> > > obj_a.multiply(2); // same as multiplier(obj_a, 2);
--
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.