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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:53:33+00:00 2026-06-02T01:53:33+00:00

I am attempting to clone an object in JavaScript. I have made my own

  • 0

I am attempting to clone an object in JavaScript. I have made my own ‘class’ that has prototype functions.

My Problem: When I clone an object, the clone can’t access/call any prototype functions.

I get an error when I go to access a prototype function of the clone:

clone.render is not a function

Can you tell me how I can clone an object and keep its prototype functions

This simple JSFiddle demonstrates the error I get: http://jsfiddle.net/VHEFb/1/

function cloneObject(obj) 
{
   // Handle the 3 simple types, and null or undefined
   if (null == obj || "object" != typeof obj) return obj;

   // Handle Date
   if (obj instanceof Date) {
     var copy = new Date();
     copy.setTime(obj.getTime());
     return copy;
   }

   // Handle Array
   if (obj instanceof Array) {
     var copy = [];
     for (var i = 0, len = obj.length; i < len; ++i) {
         copy[i] = cloneObject(obj[i]);
     }
     return copy;
   }

   // Handle Object
   if (obj instanceof Object) {
     var copy = {};
     for (var attr in obj) {
         if (obj.hasOwnProperty(attr)) copy[attr] = cloneObject(obj[attr]);
     }
     return copy;
   }

   throw new Error("Unable to copy obj! Its type isn't supported.");
}

function MyObject(name)
{
    this.name = name;
    // I have arrays stored in this object also so a simple cloneNode(true) call wont copy those
    // thus the need for the function cloneObject();
}

MyObject.prototype.render = function()
{
    alert("Render executing: "+this.name);
}

var base  = new MyObject("base");
var clone = cloneObject(base);
clone.name = "clone";
base.render();
clone.render();  // Error here: "clone.render is not a function"
  • 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-02T01:53:34+00:00Added an answer on June 2, 2026 at 1:53 am

    Some comments on the code:

    >    if (obj instanceof Date) {
    >      var copy = new Date();
    >      copy.setTime(obj.getTime());
    

    can be:

    if (obj instanceof Date) {
      var copy = new Date(obj);
    

    and

    >    if (obj instanceof Array) {
    

    will return false if obj is an array from another global context, such as an iFrame. Consider:

         if (o && !(o.constructor.toString().indexOf("Array") == -1))
    
    >      var copy = [];
    >      for (var i = 0, len = obj.length; i < len; ++i) {
    >          copy[i] = cloneObject(obj[i]);
    >      }
    

    Copying the indexes of one array to another can be done more efficiently and accurately using slice:

          var copy = obj.slice();
    

    though you will miss any other properties that might have been added that aren’t numeric. Looping over 0 to length will add properties to the clone that don’t exist in a sparse array (e.g. elisions will become undefined members).

    As for the cloning part…

    In the part copying object properties, that will copy all the properties, including those on the original’s [[Prototype]] chain, directly to the “clone” object. The only way to really “clone” an object is to set its [[Prototype]] to the same object as the original, then copy the enumerable properties on the original (filtered with hasOwnProperty) to the clone.

    The second part is trivial, the first part is not (in a general sense) since you can’t guarantee that an object’s constructor property references the object whose prototype is its [[Prototype]], nor can you guarantee that the constructor’s prototype hasn’t changed (i.e. is a different object) in the meantime.

    The closest you can get is to use Lasse Reichstein Nielsen’s clone (popularised by Douglas Crockford as beget) which makes the original object the [[Prototype]] of the clone, and then set the constructor to the same object. Though you probably still need to copy over the enumerable own properties so they mask the original’s same-named properties.

    So you can really only clone an object within a restricted context, you can’t do it generally. And generally that realisation leads to a design where you don’t need to generically clone objects.

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

Sidebar

Related Questions

I am attempting to create a login system that has two access levels. If
Attempting a beginner's tutorial. I have the following in my head: <script type=text/javascript charset=utf-8
I am attempting to compare a bitwise clone object to its parent to check
I have run into a problem attempting to redispatch mouse events in ActionScript 3,
I'm attempting to attach a listener to a window object that is destroyed when
I have data stored in a SQL database that I'm attempting to read into
I'm attempting to create a custom JButton that has interchangeable skin components. Using CardLayout
I am attempting to convert a GUI application that I made in Delphi (actually,
I am attempting to use the .clone() in jquery. I have a $.post retuning
Attempting to make a NSObject called 'Person' that will hold the login details for

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.