Functions: call / apply / f()

Discussion in 'Javascript' started by Philippe Poulard, Sep 5, 2008.

  1. 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 !
     
    Philippe Poulard, Sep 5, 2008
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Sep 5, 2008
    #2
    1. Advertising

  3. 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 !
     
    Philippe Poulard, Sep 5, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    976
    Marrow
    Jul 18, 2003
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,631
    Victor Bazarov
    Jun 21, 2004
  3. Alan G Isaac

    replay 'apply' with extended call

    Alan G Isaac, Oct 25, 2004, in forum: Python
    Replies:
    5
    Views:
    360
    Peter Otten
    Oct 25, 2004
  4. Sune
    Replies:
    8
    Views:
    706
    Keith Thompson
    Sep 26, 2005
  5. HopfZ

    Function.call and Function.apply

    HopfZ, Aug 25, 2006, in forum: Javascript
    Replies:
    1
    Views:
    99
    Lasse Reichstein Nielsen
    Aug 25, 2006
Loading...

Share This Page