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

  • Home
  • SEARCH
  • 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 6910769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:26+00:00 2026-05-27T08:51:26+00:00

I have a class that goes like this: function Element(){ this.changes = {}; }

  • 0

I have a class that goes like this:

function Element(){
    this.changes = {};
}

Now I have an instance of this “Class” like so, el = new Element(). These instances are stored in an array, like elements.push(el).

This array of elements is now stored in an object, which is then pushed in an array, states.

Now there are cases where I need a copy of one of the elements, so I would need to do something like, var cloned = $.extend(true, {}, states[0]). Here I assumed that we are cloning the first state.

The problem now is that what I get, the state[1].elements[0] is still pointing to the original instance. Thus any changes I am making to the cloned object, are changing the original too.

It’s frustrating to be stuck on such a trivial problem…

Here is a fiddle I created to test it out:
http://jsfiddle.net/E6wLW/

  • 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-27T08:51:26+00:00Added an answer on May 27, 2026 at 8:51 am

    $.extend is only cloning plain objects. If the object has a constructor then it is not cloned, but just copied.

    From the $.extend source:

    if ( jQuery.isPlainObject(copy) /* ... */) {
      // do the recursive $.extend call and clone the object                
    } else if ( copy !== undefined ) {
      target[ name ] = copy;
      // ^^^^^ just copy
    }
    

    So $.extend() will call isPlainObject(el) which will return false, because el has an constructor and instead of cloning the el is copied. So states[1].elements[0] is the same object as states[0].elements[0] because it was not cloned.

    If we modify your example from:

    function Element(){
      this.changes = {};
    }
    var el = new Element();    // $.isPlainObject(el); <- false
    // ...
    

    into:

    var el = { changes: {} };  // $.isPlainObject(el); <- true
    // ...
    

    It will clone the el properly. See it HERE.

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

Sidebar

Related Questions

I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a template class which goes like this: class Template{ public $pageTitle =
I have a sql exception from clojure that goes like this: java.lang.Exception: transaction rolled
I have some javascript that goes out and fetches a javascript class on another
I have a class that creates several IDisposable objects, all of these objects are
I have a page like this : <div id=daysTable> <div id=day0 class=day></div> <div id=day1
I have a javascript function (class) that takes a function reference as one paremter.
first some code: I have many elements like that: <section class=item> <div class=caption> </div>
I have a title on a blog goes like this Main Idea, key term,
I have class that represents users. Users are divided into two groups with different

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.