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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:09:22+00:00 2026-06-03T22:09:22+00:00

I stumbled upon a very weird behavior inside a javascript closure. If a global

  • 0

I stumbled upon a very weird behavior inside a javascript closure. If a “global” variable of a closure is a object, any assignment made from it to another variable behaves like some kind of pointer. A simple code should explain better what I mean:

function closure() {      
  var foo = {key1 : 'value 1'};
  return {
    method: function(){
      var bar = foo;
      bar.key2 = 'value2';
      return 'foo has attributes : ' + foo.key1 + ' and ' + foo.key2
    }
  }
}  
var someVar = closure();
someVar.method();  // returns "foo has attributes : value 1 and value2" (!!!)

I would obviously expect to obtain foo has attributes : value 1 and undefined as only bar has been modified… However, the script actually modifies both bar and foo with confuses me a lot.

I noticed as well that when foo and bar are strings or numbers everything works as expected – modifying bar does not affect foo.

function closure() {
  var foo = 10;
  return {
    method: function(){
      var bar = foo; 
      bar += 1;
      return 'foo is : ' + foo + ' and bar is ' + bar
    }
  }
}
var someVar = closure();
someVar.method(); // returns "foo is : 10 and bar is 11"

I spent whole evening trying to figure out the reason of this behavior… no luck. I got the same results is SpiderMonkey and V8 (both on Chrome and node.js).

What I want to do is of course to modify bar from the first example without affecting foo. Any help will be very appreciated.

  • 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-03T22:09:25+00:00Added an answer on June 3, 2026 at 10:09 pm

    When you do var bar = foo;, both bar and foo are pointing to the same object. If you don’t want foo to change, you need to clone foo. When you clone, you will get an entirely new object, and bar will point to that. This answer has some information regarding the cloning of objects in Javascript.

    Also, this behavior isn’t limited to a closure. It will happen in regular Javascript code:

    var foo = {a: 10, b: 6};
    var bar = foo; 
    bar.c = 7;
    console.log(foo);
    

    Will show foo as having the properties a, b, and c.

    What you’re seeing is standard behavior of most object-oriented languages. When you are dealing with objects, you are dealing with references to those objects instead of the specific instance-values. You don’t see this with numbers because in Javascript they are primitive types, so you’re dealing with the actual values rather than a reference to those values.

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

Sidebar

Related Questions

I stumbled upon a very weird bug and I can't explain why it happens.
I stumbled upon the function .globalEval() from browsing the jQuery source. There is very
Stumbled upon a weird Chrome-only rendering behavior for select-boxes: Select one option and during
I stumbled upon a very strange bit of PHP code. Could someone explain why
I recently stumbled upon a global hotkey class ( This one ), it works
I stumbled upon a very puzzling feature(?) of Java. It seems that using a
I stumbled upon a problem from the last Facebook Hacker Cup (so it's NOT
Okay, this is pretty weird. I made an app that listens for changes inside
While doing a lot of benchmarking stuff these days, I stumbled upon something very
I'm trying my first very basic Facebook app using php and have stumbled upon

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.