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

I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function
I have objects which create other child objects within their constructors, passing 'this' so
I've coded an experimental function which makes passed objects chainable by using high order
I have a javascript function (class) that takes a function reference as one paremter.
I'm having some trouble with JavaScript and the passing of a function as parameter
I am fairly new to JavaScript and I want to create objects and arrays
I am passing one parameter/object to the function RefValTest(object oIn) by the value but
.NET XSLT engine allows passing objects to the XSLT processing engine through the AddExtensionObject
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
I have an ArrayCollection of objects. I'm passing this array to a horizontallist as

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.