Function.call and Function.apply

Discussion in 'Javascript' started by HopfZ, Aug 25, 2006.

  1. HopfZ

    HopfZ Guest

    I would like to check if my understanding of the two functions are
    correct.

    Is this correct? The following three lines are equivalent, for
    example, [1] can be replaced with [2] or [3] anywhere in a script
    without changing the return value and without changing any side effect.


    [1] var r = o.f(a,b);
    [2] var r = o.f.call(o,a,b);
    [3] var r = o.f.apply(o,[a,b]);

    ( o is an object and o.f is a function. )

    The following three lines are equivalent:
    [1'] var r = g(a,b);
    [2'] var r = g.call(this,a,b);
    [3'] var r = g.apply(this,[a,b]);

    ( g is a function, for example,
    var g = function(a,b){return [this,a,b];}. )
    HopfZ, Aug 25, 2006
    #1
    1. Advertising

  2. "HopfZ" <> writes:

    > Is this correct? The following three lines are equivalent

    ....
    > [1] var r = o.f(a,b);
    > [2] var r = o.f.call(o,a,b);
    > [3] var r = o.f.apply(o,[a,b]);

    Correct


    > The following three lines are equivalent:
    > [1'] var r = g(a,b);
    > [2'] var r = g.call(this,a,b);
    > [3'] var r = g.apply(this,[a,b]);


    Only if used where "this" refers to the global object (i.e., the call
    is in the global scope or inside a function called in this way).

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Aug 25, 2006
    #2
    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:
    951
    Marrow
    Jul 18, 2003
  2. Alan G Isaac

    replay 'apply' with extended call

    Alan G Isaac, Oct 25, 2004, in forum: Python
    Replies:
    5
    Views:
    349
    Peter Otten
    Oct 25, 2004
  3. Andrey Fedorov
    Replies:
    9
    Views:
    277
    Carl Banks
    Sep 10, 2009
  4. Andrew Thelwell
    Replies:
    0
    Views:
    111
    Andrew Thelwell
    May 17, 2007
  5. Philippe Poulard

    Functions: call / apply / f()

    Philippe Poulard, Sep 5, 2008, in forum: Javascript
    Replies:
    2
    Views:
    84
    Philippe Poulard
    Sep 5, 2008
Loading...

Share This Page