Thanks a lot T.J.
I will read your answer carefully later on, but it looks very complete and
clear
I really appreciate your help.
---
Omar Adobati
On Wed, Mar 24, 2010 at 15:08, T.J. Crowder t...@crowdersoftware.com wrote:
Hi Omar,
In the general case, you can reference a function simply by using its
name whenever it's in scope, which is to say, if you could call it.
So:
function foo() {
alert(Hi from foo!);
}
function bar(func) {
func();
}
foo();
bar(foo);
...results in two alerts, the first from `foo` directly, the second
from calling `bar` and giving it a reference to `foo` as an argument,
which it then calls. (In this example, it happens that `bar` could
also have just called `foo` directly, but ignore that.) Note that when
we called `bar`, *we* didn't call `foo` (there are no parentheses
after it), we passed a *reference* to it into `bar` (which then called
it -- but it didn't have to, it could have remembered it to call later
for some reason).
A function is in scope if it's defined in the current function or any
containing function (with top level global space effectively being a
function). So:
function charlie() {
bar(foo);
function foo() {
alert(Hi from foo!);
}
}
function bar(func) {
func();
}
charlie(); // = That's fine, shows alert
foo(); // = syntax error, `foo` is undefined
bar(foo); // = syntax error, `foo` is undefined
In the above, `foo` is defined within `charlie` but not outside of it.
If we defined other functions within `charlie`, they'd also have
access to `foo`. `bar` doesn't have access to `foo`, but when
`charlie` calls `bar` it passes in the function reference to `foo` as
an argument. `bar` has access to its arguments (obviously!) and so can
call the function that way. (A function within a function is sometimes
called a closure because it has access to the data in the function
that surrounds it. `foo` is a closure within `charlie`.)
In your specific situation, I'm not clear what it is you're trying to
achieve with those arguments, so I can't really help with what
function references you'd use. Also, very few of your functions
actually have names (most of them are anonymous functions bound to
object properties -- the properties have names, but the functions
don't, so you can only refer to the functions via the object
properties). I think that if you're subclassing Ajax.Request, you
wouldn't need to pass arguments at that point in the options if you're
trying to refer to the functions that your subclass has attached to
properties with those names; you'd use them directly in the subclass,
e.g.:
initialize: function($super, url, options) {
var opts;
// Create our options, starting with what the user supplied
opts = Object.extend({}, options);
// ...and adding in our handlers
opts.onSuccess = this.success.bind(this);
opts.onException = this.error.bind(this);
/* ...etc... */
// Set some other stuff
this.setUserID(options.usr);
this.setPassword(options.pwd);
this.setType(options.type);
// Determine the URL
var url = this.setUrl(this.getUserID(), this.getPassword(),
this.getType());
// Pass our updated options on to $super
$super(url, opts);
}
...but that's kind of a guess. (Also note that I'm using Function#bind
to ensure that when your handlers get called, they get called with the
right `this` value -- because you're using an instance, so I'm
guessing you want access to the instance data.)
There's a lot of info and jargon above, sorry about that. These blog
posts may be useful:
Regarding scope, there's some here:
http://blog.niftysnippets.org/2008/03/when-you-absolutely-positively-need-new.html
...and a *lot* more here, as well as more information on closures:
http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html
Regarding function names vs. property names:
http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html
And separately, the API docs for Function#bind:
http://api.prototypejs.org/language/function.html#bind-instance_method
HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com
On Mar 24, 1:11 pm, 0m4r omar.adob...@gmail.com wrote:
Wow, thanks T.J.!!
your answer makes a lot of sense.. but, now, how I am supposed to pass
a function name instead of a string?
I'm sorry if the question sounds stupid to you but I don't know how to
achieve this target.
Thanks
Omar
On Mar 24, 12:55 pm, T.J. Crowder t...@crowdersoftware.com wrote:
Hi,
In this code:
function click() {
var opt = {
method: 'get',
onCreate: 'create',