Solved

Generate a Copy of a Function

Posted on 2008-06-23
6
1,104 Views
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
<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
Comment
Question by:AWarrenM
  • 4
  • 2
6 Comments
 

Author Comment

by:AWarrenM
Comment Utility
increased point value.
0
 
LVL 42

Expert Comment

by:David S.
Comment Utility
Which browser(s) did you get that error message in?
0
 

Author Comment

by:AWarrenM
Comment Utility
Firefox for right now. Didn't test it in IE.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:AWarrenM
Comment Utility
Bumping again..
0
 
LVL 42

Accepted Solution

by:
David S. earned 300 total points
Comment Utility
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
 

Author Comment

by:AWarrenM
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now