Modifying a existing function

V

VA

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.
 
R

RobG

VA said:
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>



[...]
 
V

VA

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
 
T

Thomas 'PointedEars' Lahn

VA said:
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
 
I

Ian Osgood

VA said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top