Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:48+00:00 2026-05-22T20:49:48+00:00

EDITED Note: I’ve completely edited this because previous problem was solved using another approach

  • 0

EDITED

Note: I’ve completely edited this because previous problem was solved using another approach and I’ve simplified this question in order to answer myself with my solution to the question and share my conclusion with you.

How to clone a function by avoiding the use of eval(...)?

I need to get an exact copy of some given function and this must be a different object than source one.

That’s functionA != functionCloneOfA, and wrapping “functionA” and calling it from the body of some other isn’t a solution for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:49:49+00:00Added an answer on May 22, 2026 at 8:49 pm

    Finally I didn’t need to clone functions to solve some problem in one of my projects, but I want to share with you what I found in order to truly clone a function:

    function cloneFunc(someFunc) {
        var someFuncAsText = someFunc.toString();
    
        return new Function
        (
            /\(([\s\S]*?)\)/.exec(someFunc)[1],
            someFuncAsText.substring(someFuncAsText.indexOf("{") + 1, someFuncAsText.lastIndexOf("}"))
        );
    }
    

    I’m not absolutely sure this is the best approach around, but it produces an anonymous function which is an exact clone of source function, but both are different objects.

    Perhaps uses some kind of “eval” because I’m building a function by using this Function object constructor, but I believe it’s more elegant than an actual use of eval(…).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
Note: I edited this question to make it easier for other people with the
Note: I've cross-posted this question in the grails-user mailing list This weekend using this
Note: Edited the sample to reflect my actual problem, which was a trailing comma
Moderator note: This question is not a good fit for our question and answer
Note: question has been edited to stay in sync with what I have tried
Using this example coming from Wikipedia, in which DrawSquare() calls DrawLine() : (Note that
NOTE: EDITED The real-world situation is a series of events that each have two
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
--edited for clarity (hopefully) I have an XML file that looks something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.