469,645 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Modifying a existing function

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.

Oct 24 '05 #1
4 2008
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
Oct 24 '05 #2
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

Oct 24 '05 #3
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
Oct 24 '05 #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.

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.

Oct 24 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jeffc | last post: by
7 posts views Thread by Jan Gregor | last post: by
2 posts views Thread by MikeY | last post: by
3 posts views Thread by Chris Bingham | last post: by
13 posts views Thread by Robin Becker | last post: by
4 posts views Thread by bp90210 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.