Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » JavaScript

Functions: call / apply / f()

 
Jump to:  
 
Philippe Poulard
PostPosted: Fri Sep 05, 2008 6:21 am    Post subject: Functions: call / apply / f()
       
Hi folks !

I have some code like this that passes an anonymous function:
foo.bar(p1, function(p2) {
alert(p2);
});

in foo.bar, i create a button with a listener; when it is executed, the
following code fails to run:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction(someData);
});
...
}

I google for some documentation and this version works well:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction.call(null, someData);
});
...
}

I don't understand the difference between f.apply(), f.call() and f()
and why the latter doesn't work

In fact, when using f("foo") the function is executed (I'm sure of that
when I insert alert("Hello")), but without its argument if in the body
of the anonymous function i use alert(p2) it fails

Please can you explain this behaviour ?

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
LINK
Have the RefleX !
 

 
Martin Honnen
PostPosted: Fri Sep 05, 2008 8:40 am    Post subject: Re: Functions: call / apply / f()
       
Philippe Poulard wrote:

Quote:
I have some code like this that passes an anonymous function:
foo.bar(p1, function(p2) {
alert(p2);
});

in foo.bar, i create a button with a listener; when it is executed, the
following code fails to run:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction(someData);
});
...
}

Do you get an error? Which one exactly?

Quote:
I google for some documentation and this version works well:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction.call(null, someData);
});
...
}

I don't understand the difference between f.apply(), f.call() and f()
and why the latter doesn't work

In fact, when using f("foo") the function is executed (I'm sure of that
when I insert alert("Hello")), but without its argument if in the body
of the anonymous function i use alert(p2) it fails

Please can you explain this behaviour ?

So the alert dialog does not appear? Have you checked the error console
of the browser?
I don't see a reason why f() should fail but f.call() should execute.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 

 
Philippe Poulard
PostPosted: Fri Sep 05, 2008 10:28 am    Post subject: Re: Functions: call / apply / f()
       
Martin Honnen a écrit :
Quote:
Philippe Poulard wrote:

I have some code like this that passes an anonymous function:
foo.bar(p1, function(p2) {
alert(p2);
});

in foo.bar, i create a button with a listener; when it is executed, the
following code fails to run:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction(someData);
});
...
}

Do you get an error? Which one exactly?

No because it is trapped by the framework within which it is executed
(that doesn't log it correctly); but it fails because i don't get the
alert box (I'm sure the block is executed because if I insert
alert("Hello"); before alert(p2); I see the former but not the latter)

Therefore, it seems that a reference to p2 make all the stuff crashing

Quote:

I google for some documentation and this version works well:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction.call(null, someData);
});
...
}

I don't understand the difference between f.apply(), f.call() and f()
and why the latter doesn't work

In fact, when using f("foo") the function is executed (I'm sure of
that when I insert alert("Hello")), but without its argument if in the
body of the anonymous function i use alert(p2) it fails

Please can you explain this behaviour ?

So the alert dialog does not appear? Have you checked the error console
of the browser?
I don't see a reason why f() should fail but f.call() should execute.

ok, so it's not my fault Smile
since f.call() works, I have changed to it in my code

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
LINK
Have the RefleX !
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Przystawki ubezpieczenie samochodu wózki widłowe Parkiet lokata