Forum Moderators: open

Message Too Old, No Replies

Prototype, subfunctions, and variable scope

         

Nutter

10:24 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Say I've got the following sort of code to do an Ajax call with the Prototype library.

function myFunction(url, myVariable)
{
new Ajax.Request(url, {method:'post', parameters="value=somevalue",
onSuccess:function()
{
alert(myVariable);
});
}

I want to be able to use the myVariable variable that's part of the function line in the onSuccess function, but it seems that the scope doesn't work. When I try to use it, the variable is undefined.

Is there a way to use it? I'm sure I'm missing something easy.

Fotiman

11:18 pm on Jan 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't really looked that closely but it appears as though you're missing a closing }.

function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters="value=somevalue",
onSuccess:function()
{
alert(myVariable);
}
);
}

Should be:


function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters="value=somevalue",
onSuccess:function()
{
alert(myVariable);
}
}
);
}

Don't know if that's your problem or not... no time to look close right now.

Nutter

11:28 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



I probably am missing a bracket in what I put here, but the real function (a much longer version than what I typed here) works. It just can't get the value of myVariable. What I've done for now is pass the value of myVariable to the PHP script that then sends it back as part of the return string. Seems like an inefficient way to do it, but it works for now.

gergoe

1:56 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



This particular topic is not clear to me either (how variable scope works in such a case, when nameless functions are involved for example), but I have done a quick test and for me it worked fine, I defined a nameless function within an another one (well, actually it become a sort of a class), and I could call it properly as long as the (second) function was defined inside the (first) function. So either it is a browser incompatibility issue, or the library you are using does something which makes it go wrong. To go around the problem you could save your variable into an another scope, for example to the window? Not a nice solution, but would definitely make it work :-)

Nutter

6:37 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



How would I save it into another scope? While that may be a messy solution, it's still better than what I've got working now.

And thank you for calling it a "nameless" function. I've been looking for information but didn't know that term so I was having a hard time finding anything.

[edited by: Nutter at 6:38 pm (utc) on Jan. 14, 2008]

mehh

7:14 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



to save to another scope just use
window.myVar
. That should be able to be referenced to anywere.

I think your code is failing because of a syntax error though

function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters="value=somevalue",// JSON string so = isn't valid
onSuccess:function()
{
alert(myVariable);
}
}
);
}

try:
function myFunction(url, myVariable) 
{
new Ajax.Request
(
url,
{
method:'post',
parameters:'value=somevalue',
onSuccess:function()
{
alert(myVariable);
}
}
);
}

Also if your interested in "nameless" functions they may also be referfed anonymous functions.

Quick note on scope

class.prototype={
hello:"hi" //hello belongs to class.prototype
foo:function (){
var bar="foo" //bar belongs to class.prototype.foo
global // variable not defined in class.prototype.foo assumed to be a global
this // refers to class.prototype or an object constructed by new class();
}
}

Fotiman

4:58 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




And thank you for calling it a "nameless" function. I've been looking for information but didn't know that term so I was having a hard time finding anything.

The actual term is "anonymous function".
Edit: I see mehh already mentioned this. Should have read further. :-)

[edited by: Fotiman at 5:00 pm (utc) on Jan. 15, 2008]