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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:28:35+00:00 2026-06-18T03:28:35+00:00

This code: function CHANGE(d, cb){ d.three = 3; d = {}; console.log(d was changed

  • 0

This code:

function CHANGE(d, cb){
    d.three = 3;
    d = {};
    console.log("d was changed into:");
    console.log(d);
    cb();
  }

  var d = { one: 1, two: 2 };
  CHANGE(d, function(){
    console.log("d after the callback is: ");
    console.log(d);
  });

Since d is rewritten in CHANGE(), really ought to show {} as d. However, the result is bizarre:

d was changed into:
{}
d after the callback is: 
{ one: 1, two: 2, three: 3 }

So, d had the property three added to it. And that’s OK. Then it was assigned an empty object {}.

I don’t know what to think.

An extra attribute be added in the callback (three), fine.
Then the object is rewritten. Fine. The printout shows that the object was indeed rewritten.
Then, in the callback, it shows the object with the extra three attribute…?!?

It would be consistent if it were either unchanged, or rewritten. But… half changed…?

In fact, there is no need to use a callback to show this. Even this has the same problem:

function CHANGE( o ){
   o.three = 3;
   o = {};
 }

 var d = { one: 1, two: 2 };
 CHANGE(d);
 console.log("d after the function is: ");
 console.log(d);

Here d is again the full object including three.
So, any particular reason why I cannot change the object reference altogether, but can change attributes…?

Merc.

  • 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-18T03:28:36+00:00Added an answer on June 18, 2026 at 3:28 am
    d = {};
    

    This redefines your variable to a new object, so you lose the old reference. This means that the original isn’t altered after this point.

    EDIT:

    If you want to clear an existing object of its properties and reassign some others, you could use this:

    var a = {some:"stuff", other: "stuff"};
    
    function clearProperties(obj) {
        for (var i in obj) {
             delete obj[i];
        }
    }
    
    console.log(a);
    clearProperties(a);
    a.newStuff = "stuff";
    console.log(a);
    

    Output:

    Object {some: "stuff", other: "stuff"} fiddle.jshell.net:28
    Object {newStuff: "stuff"} 
    

    http://jsfiddle.net/2DxzT/

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

Sidebar

Related Questions

I dropped this code into Chrome. setInterval( function() { console.log( $('#element').css('font-size') ); }, 500
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I found this code posted on SO to change text links into hyperlinks: function
This is my JavaScript code: <script> function change(name){ var element = document.all.name.value; } </script>
I have the next code: js: $('[id^=check-]').change(function(){ var new_id = this.id.replace(/check/, 'new'), real_id =
Is this possible? example: $('a.change').click(function(){ //code to change p tag to h5 tag });
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have this code: function Keyboard() { this.log = $('#log')[0]; this.pressedKeys = []; this.bindUpKeys
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some

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.