Go Premium for a chance to win a PS4. Enter to Win


Generate a Copy of a Function

Posted on 2008-06-23
Medium Priority
Last Modified: 2008-06-25
I'm looking to copy a function, override it, and use the copy where I overrided the function.

For example:
file: fns.js, function test(a, b){ return (a+b)};
file: index.html
var oldFn = test;

function test(a,b) {
  return oldFn(a+2, b-2);

index.html is including fns.js.

The error I'm seeing is "Too many recursive calls." Which is leading me to believe that the oldFn var is being replaced with the new declration of "test", so it's calling itself.

I've done a lot of searching and haven't found anything that could help. There's a lot of examples of object copying (which I've tried, with no luck), but nothing with function copying.

Thanks in advance
Question by:AWarrenM
  • 4
  • 2

Author Comment

ID: 21851556
increased point value.
LVL 43

Expert Comment

by:David S.
ID: 21852563
Which browser(s) did you get that error message in?

Author Comment

ID: 21856153
Firefox for right now. Didn't test it in IE.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 21862160
Bumping again..
LVL 43

Accepted Solution

David S. earned 1200 total points
ID: 21862196
Ah. I should have tested with your exact code earlier.

Try using an anonymous function when you redefine it:
function test(a, b){ return (a+b)};
var oldFn = test;
test=function(a,b) {
  return oldFn(a+2, b-2);

Open in new window


Author Comment

ID: 21867768
Wow Krav.. I thought I tried that. But I looked back and noticed I had  test  = new function...

Silly silly.

Thank you.

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question