how to override a function?

Discussion in 'Javascript' started by mitch, Apr 26, 2006.

  1. mitch

    mitch Guest

    Is there a way to reassign a function? I tried this:

    function func() { alert("func"); }
    function func2() { alert("func2"); }

    function testFunc() {
    document.func = func2;
    func();
    }

    and it prints "func" not "func2". I'd like to be able to override the
    global function "func" to make it do something else. Is there a
    trick to do it? Thanks.

    The reason I want to do it is for unit testing. I have some test
    code I'm writing, and I want to call the production code and override
    some of the functions in the production code, making other
    functions unit-testable. I would prefer to do it without changing
    the production code.
     
    mitch, Apr 26, 2006
    #1
    1. Advertising

  2. mitch wrote:

    > Is there a way to reassign a function? I tried this:
    >
    > function func() { alert("func"); }
    > function func2() { alert("func2"); }
    >
    > function testFunc() {
    > document.func = func2;

    window.func = func2;
    > func();



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 26, 2006
    #2
    1. Advertising

  3. mitch wrote:
    > Is there a way to reassign a function? I tried this:
    >
    > function func() { alert("func"); }
    > function func2() { alert("func2"); }
    >
    > function testFunc() {
    > document.func = func2;
    > func();
    > }
    >
    > and it prints "func" not "func2". I'd like to be able to override the
    > global function "func" to make it do something else. Is there a
    > trick to do it? Thanks.


    func = func2 will do it.

    Of course then you've lost the original func function.

    (The default owner object is window, not document, so window.func =
    func2 means the same thing)

    ExG
     
    TheBagbournes, Apr 26, 2006
    #3
  4. mitch

    mitch Guest

    Thanks, window.func worked.

    - Mitch
     
    mitch, Apr 26, 2006
    #4
  5. mitch

    RobG Guest

    mitch said on 27/04/2006 2:35 AM AEST:
    > Is there a way to reassign a function? I tried this:
    >
    > function func() { alert("func"); }
    > function func2() { alert("func2"); }
    >
    > function testFunc() {
    > document.func = func2;
    > func();
    > }
    >
    > and it prints "func" not "func2". I'd like to be able to override the
    > global function "func" to make it do something else. Is there a
    > trick to do it? Thanks.
    >
    > The reason I want to do it is for unit testing. I have some test
    > code I'm writing, and I want to call the production code and override
    > some of the functions in the production code, making other
    > functions unit-testable. I would prefer to do it without changing
    > the production code.


    In addition to other responses, functions declared in the global scope
    belong to the global object. In browsers, the global object is
    synonymous with the window object.

    However, not all user agents are browsers and therefore may not have a
    window object. To be confident of explicitly addressing global objects
    and properties, you may want to create your own reference to the global
    object (usually at the beginning of the script):

    var _global = this;


    Now you can use:

    function testFunc() {
    _global.func = _global.func2;
    _global.func();
    }


    In some situations that may be good practice, but usually:

    function testFunc() {
    func = func2;
    func();
    }


    will do.


    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/FAQ>
     
    RobG, Apr 27, 2006
    #5
    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. Angus Comber
    Replies:
    3
    Views:
    839
    John Harrison
    Apr 10, 2004
  2. Angus Comber
    Replies:
    3
    Views:
    898
    John Harrison
    Apr 11, 2004
  3. David
    Replies:
    2
    Views:
    522
    David
    Jun 27, 2004
  4. Replies:
    2
    Views:
    971
    Bengt Richter
    Aug 1, 2005
  5. Peng Yu
    Replies:
    4
    Views:
    377
    peter koch
    Sep 20, 2008
Loading...

Share This Page