Modifying a existing function

Discussion in 'Javascript' started by VA, Oct 24, 2005.

  1. VA

    VA Guest

    I am using a JS library of functions that I include in my web page
    using the usual
    <script type="text/javascript" src=...>
    method

    Is there a way to automatically call one of my own functions when
    function foo(...) (from the library) is called?

    In other words, when foo() is executed, check to see if there is an
    "execute me also" type of function (associated with foo) defined and if
    so, execute that BEFORE foo() starts execution?

    Ideally, I would like to do this without modifying the source code for
    the function foo.

    Hope I am making sense.

    Thanks for any help.
     
    VA, Oct 24, 2005
    #1
    1. Advertising

  2. VA

    RobG Guest

    VA wrote:
    > I am using a JS library of functions that I include in my web page
    > using the usual
    > <script type="text/javascript" src=...>
    > method
    >
    > Is there a way to automatically call one of my own functions when
    > function foo(...) (from the library) is called?
    >
    > In other words, when foo() is executed, check to see if there is an
    > "execute me also" type of function (associated with foo) defined and if
    > so, execute that BEFORE foo() starts execution?
    >
    > Ideally, I would like to do this without modifying the source code for
    > the function foo.


    Assign foo() a new name, then assign a new function to foo that does
    your new functionality, then runs the original (now re-named) foo.

    It doesn't modify the internal workings of foo(), it runs the pre-pended
    stuff first, then runs foo.


    <html><head><title>foo() play</title>
    <script type="text/javascript">

    function foo()
    {
    alert('I am foo');
    }

    function changeFoo()
    {
    var x = foo;
    foo = function() {
    alert('Some added stuff from new foo()');
    x();
    }
    alert('foo() has been modified');
    }

    </script>

    </head>
    <body>
    Click Run foo(), then Modify foo, then Run foo() again.<br>
    <input type="button" value="Run foo()" onclick="foo();">
    <input type="button" value="Modify foo()" onclick="changeFoo();">

    </body>
    </html>



    [...]


    --
    Rob
     
    RobG, Oct 24, 2005
    #2
    1. Advertising

  3. VA

    VA Guest

    OK but what about arguments to the foo()? It is like a black box to me,
    I dont know its arguments, I dont have their values.

    Any ideas? Thanks
     
    VA, Oct 24, 2005
    #3
  4. VA wrote:

    > OK but what about arguments to the foo()? It is like a black box to me,
    > I dont know its arguments, I dont have their values.


    Probably a good reason why you should not use it in the first place.

    Seriously, all client-side JS code is available as source code, otherwise it
    would not be usable. Maybe it is packed or encoded in some way but it is
    still plain text included in the external script file. In your manner of
    speaking, it is not a black box at all.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Oct 24, 2005
    #4
  5. VA

    Ian Osgood Guest

    VA wrote:
    > OK but what about arguments to the foo()? It is like a black box to me,
    > I dont know its arguments, I dont have their values.
    >
    > Any ideas? Thanks


    You can access the arguments of a function as an object and use the
    Function method "apply" to use them:

    <html><head><title>foo() play</title>
    <script type="text/javascript">

    function foo(arg)
    {
    alert('I am foo: ' + arg);
    }

    function changeFoo()
    {
    var old = foo;
    foo = function() {
    alert('Some added stuff from new foo() with ' + arguments.length
    + ' arguments');
    old.apply(this,arguments);
    }
    alert('foo() has been overridden');
    }

    </script>

    </head>
    <body>
    Click Run foo(), then Modify foo, then Run foo() again.<br>
    <input type="button" value="Run foo('bar')" onclick="foo('bar');">
    <input type="button" value="Run foo(1,2)" onclick="foo(1,2);">
    <input type="button" value="Modify foo()" onclick="changeFoo();">

    </body>
    </html>

    Note that you can modify foo() multiple times, each time it will link
    another function in front.
     
    Ian Osgood, Oct 24, 2005
    #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. Peter D. Dunlap

    Modifying the Back Button Function

    Peter D. Dunlap, Jul 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    447
    Cowboy \(Gregory A. Beamer\)
    Jul 3, 2003
  2. nrm
    Replies:
    3
    Views:
    580
  3. rk
    Replies:
    2
    Views:
    751
    Joe Kesselman
    Jun 15, 2006
  4. Ed Keith
    Replies:
    2
    Views:
    377
    John Machin
    Dec 22, 2010
  5. Replies:
    0
    Views:
    217
Loading...

Share This Page