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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:33:30+00:00 2026-06-06T18:33:30+00:00

I am trying to copy a property by value so that different instances can

  • 0

I am trying to copy a property by value so that different instances can modify it separately. My understanding is that using jQuery or Zepto’s $.extend is a great way to do ‘copy by value’. It is not working in my situation and I’d like to understand why. I can’t tell what I’m doing wrong.

var c = [ {'purple' : 1}, { 'red':2 } ]
var x = { 'name': 'my obj', 'colors': c }
var doWork = function(passX) {
  var inY = $.extend({},passX);
  inY.colors[0].selected = true;
  return inY;
}
var y = doWork(x);

console.log( x.colors[0].selected );// true, but I think it should be undefined
console.log( y.colors[0].selected );​// true

I feel like I might be missing something really central. Can I not extend from the same object to do a copy? Is there something confounding about the function argument scope?

jsFiddle: http://jsfiddle.net/zfnyp/5/

EDIT: The answer to my confusion, as @Niko points out, is that deep copy makes copy-by-value versions of all child properties. I thought deep vs shallow copy just meant how many properties deep the copy went. Oops.

EDIT AGAIN: Deep copy is troublesome in Javascript. JQuery has it, but Zepto and Underscore do not. Some describe it as impossible to implement well. To implement this for my problem, I have created this solution which relies on knowing the structure of the object. I believe this is the correct answer for me, though it is clunky.

var c = [ {'purple' : 1, 'selected':false }, { 'red':2 } ]
var x = { 'name': 'my obj', 'colors': c }
var doWork = function(passX) {
  var inY = $.extend({},passX);
  inY.colors = $.extend([], passX.colors);
  for (var i = 0; i < passX.colors.length; i++) {
    inY.colors[i] = $.extend({}, passX.colors[i]);
  }
  inY.colors[0].selected = true;
  return inY;
}
var y = doWork(x);

console.log( x.colors[0].selected );
console.log( y.colors[0].selected );
  • 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-06T18:33:32+00:00Added an answer on June 6, 2026 at 6:33 pm

    x = y makes x and y reference the same object, therefore y.colors also overwrites x.colors – the $.extend() is pretty useless in this case.

    var x = { 'name': 'my obj', 'colors': c };
    var y = $.extend(true, {}, x); // <-- first param "true" = make deep copy,
                                   //     because otherwise "x.colors === y.colors"
    

    The deep copy is required, because an array is also an object and therefore copied by reference when doing a shallow copy.

    Or with the new code:

    var inY = $.extend(true, {}, passX); // deep copy = also copy mod.colors by value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an extension method that I can use to copy values
I'm trying to get the value of an inherited CSS property using Javascript. I
I've copy-pasted a function that I'm trying to modify. I'd like to make it
I am trying to copy files from a directory that is in constant use
I'm trying to copy files to C:\Windows\System32 using cygwin bash. When I copy files
I'm currently trying to copy data from table into another by using a SELECT
I am trying to copy some property values from one object to another (both
I'm trying to copy and past a form within a project using Visual Studio.
I'm trying copy a single file from the Plugin directory inside of my Wordpress
Trying to copy the msdn refernce here doesn't work and gives an error 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.