Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1117
  • Last Modified:

Generate a Copy of a Function

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
<script>
var oldFn = test;

function test(a,b) {
  return oldFn(a+2, b-2);
}
</script>

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
0
AWarrenM
Asked:
AWarrenM
  • 4
  • 2
1 Solution
 
AWarrenMAuthor Commented:
increased point value.
0
 
David S.Commented:
Which browser(s) did you get that error message in?
0
 
AWarrenMAuthor Commented:
Firefox for right now. Didn't test it in IE.
0
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!

 
AWarrenMAuthor Commented:
Bumping again..
0
 
David S.Commented:
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

0
 
AWarrenMAuthor Commented:
Wow Krav.. I thought I tried that. But I looked back and noticed I had  test  = new function...

Silly silly.

Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now