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 7848001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:03:43+00:00 2026-06-02T18:03:43+00:00

What I am trying to do is build a clone() function for Javascript objects.

  • 0

What I am trying to do is build a clone() function for Javascript objects. As you know the lack of native functionality for this in Javascript can be a pain. I have a great method to do it, but usability wise, it is awkward. Here is the function so far:

function clone(objectToClone) {
    var myClone = function() {};
    myClone.prototype = objectToClone;
    return [
        new myClone(),
        new myClone()
    ];
}

As you can see, currently I just return an array with both clones. This makes the usage look something like this:

// usage
var bar = new foo();
bar = clone(bar); // forks bar into two separate copies of bar (awkward)
// bar[0] is now clone 1
// bar[1] is now clone 2

What I would like to do, is be able to do something like the following:

function clone(objectToClone) {
    var myClone = function() {};
    myClone.prototype = objectToClone;
    objectToClone = new myClone(); // somehow magically?
    return new myClone();
}

// usage
var bar = new foo();
var baz = clone(bar); // returns a copy, and bar is now one of the copies as well.
// bar is now clone 1
// baz is now clone 2

Unfortunately this does not work, since assigning the argument does not affect the calling scope. I was hoping that someone might be able to help think up some js trickery that would allow me to use the function in the desired manor however. Any ideas are greatly appreciated.

  • 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-06-02T18:03:44+00:00Added an answer on June 2, 2026 at 6:03 pm

    To get your desired result, you would need to pass in an object with properties and then modify those properties. That’s the best way to get a reference in javascript that allows you to change the original. You cannot change the original when it’s just passed as an ordinary function argument. Javascript doesn’t have those types of references.

    function clone(holder) {
        var myClone = function() {};
        myClone.prototype = holder.input;
        holder.input = new myClone(); // somehow magically?
        return new myClone();
    }
    
    // usage
    var holder = {};
    var holder.input = new foo();
    var holder.output = clone(holder); // returns a copy, and holder.input is now a copy too
    // holder.input is now clone 1
    // holder.putput is now clone 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build Xuggler under Windows. Xuggler is core native code functions wrapped into
I've been trying to build a site similar in function to craigslist in Drupal.
I'm trying to improve my Javascript coding style and have been reading that it's
I'm trying to build up a simple JQuery function : A modal box (one
I'm working on a Titanium app and have build a function to run sqlite
I'm trying to build a function that calculates hashed for a file. The function
I am trying to build a break-out game clone in Qt . I need
I'm trying build a method which returns the shortest path from one node to
I'm trying build an App Engine connected Android application and am having some problems
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I

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.