Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Functions: call / apply / f()

Reply
Thread Tools

Functions: call / apply / f()

 
 
Philippe Poulard
Guest
Posts: n/a
 
      09-05-2008
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 |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-05-2008
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?

> 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/
 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      09-05-2008
Martin Honnen a écrit :
> 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

>
>> 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
since f.call() works, I have changed to it in my code

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function.call and Function.apply HopfZ Javascript 1 08-25-2006 06:26 PM
Wireless Only Desktop - Network Starts too late and AD Computer Policies Don't Apply Kenny Wireless Networking 3 11-08-2005 10:53 PM
replay 'apply' with extended call Alan G Isaac Python 5 10-25-2004 03:15 PM
[Vb.net question] how to apply online update function into program (the effect just like Norton system work live update) chan ASP .Net 1 03-04-2004 02:58 PM
[XSLT] could not apply "apply-templates" Stefan Siegl XML 1 07-18-2003 09:43 AM



Advertisments