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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:35:28+00:00 2026-05-25T20:35:28+00:00

When passing objects as parameters, JavaScript passes them by reference and makes it hard

  • 0
  • When passing objects as parameters, JavaScript passes them by reference and makes it hard to create local copies of the objects.

    var o = {};
    (function(x){
        var obj = x;
        obj.foo = 'foo';
        obj.bar = 'bar';
    })(o)
    

    o will have .foo and .bar.

  • It’s possible to get around this by cloning; simple example:

    var o = {};
    
    function Clone(x) {
       for(p in x)
       this[p] = (typeof(x[p]) == 'object')? new Clone(x[p]) : x[p];
    }
    
    (function(x){
        var obj = new Clone(x);
        obj.foo = 'foo';
        obj.bar = 'bar';
    })(o)
    

    o will not have .foo or .bar.


Question

  1. Is there a better way to pass objects by value, other than creating a local copy/clone?
  • 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-25T20:35:29+00:00Added an answer on May 25, 2026 at 8:35 pm

    Not really.

    Depending on what you actually need, one possibility may be to set o as the prototype of a new object.

    var o = {};
    (function(x){
        var obj = Object.create( x );
        obj.foo = 'foo';
        obj.bar = 'bar';
    })(o);
    
    alert( o.foo ); // undefined
    

    So any properties you add to obj will be not be added to o. Any properties added to obj with the same property name as a property in o will shadow the o property.

    Of course, any properties added to o will be available from obj if they’re not shadowed, and all objects that have o in the prototype chain will see the same updates to o.

    Also, if obj has a property that references another object, like an Array, you’ll need to be sure to shadow that object before adding members to the object, otherwise, those members will be added to obj, and will be shared among all objects that have obj in the prototype chain.

    var o = {
        baz: []
    };
    (function(x){
        var obj = Object.create( x );
    
        obj.baz.push( 'new value' );
    
    })(o);
    
    alert( o.baz[0] );  // 'new_value'
    

    Here you can see that because you didn’t shadow the Array at baz on o with a baz property on obj, the o.baz Array gets modified.

    So instead, you’d need to shadow it first:

    var o = {
        baz: []
    };
    (function(x){
        var obj = Object.create( x );
    
        obj.baz = [];
        obj.baz.push( 'new value' );
    
    })(o);
    
    alert( o.baz[0] );  // undefined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

.NET XSLT engine allows passing objects to the XSLT processing engine through the AddExtensionObject
Passing an undimensioned array to the VB6's Ubound function will cause an error, so
I'm passing a reference of a form to a class. Within this class I
If I am passing an object to a method, why should I use the
I am trying to use a business object that I am passing to the
I'm doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects
I'm passing raw HTTP requests to an apache server (received by PHP). The request
When you're passing variables through your site using GET requests, do you validate (regular
I'm currently passing the pid on the command line to the child, but is
I am parsing XML with an XMLReader using a XMLReaderSettings object with the event

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.