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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:39:33+00:00 2026-05-24T08:39:33+00:00

Why is that this code: var foo = {one: 1, two: 2}; var bar

  • 0

Why is that this code:

var foo = {one: 1, two: 2};
var bar = new Object( foo );
bar.three = 3;
bar.one = 100; 
document.write(bar.one); //100 
document.write(foo.one); //100

results in bar.one & foo.one being both 100, while

var foo = {one: 1, two: 2};
var bar = Object.create( foo );
bar.three = 3;
bar.one = 100; 
document.write(bar.one); //100
document.write(foo.one); //1

only affects bar.one..

My first intuition is that since in the first piece of code we are assigning a foo reference to bar, then it means the change will also apply to foo, while on the second code, it probably ‘inherits’ from foo, and therefore the change on bar’s ‘subclass’ attribute won;t apply to its ‘superclass’ (prototype)..

Can somebody please confirm that my assumption is at least on the right track? Would absolutely appreciate any answers. Thanks in advance.

  • 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-24T08:39:34+00:00Added an answer on May 24, 2026 at 8:39 am

    The line:

    var bar = new Object( foo );
    

    In your first snippet, it doesn’t do anything -you are right with your assumption-, it will simply return a reference to the same object passed to the Object constructor.

    That’s the behavior when you pass a native object to the Object constructor in a new expression (new Object(value)), if you pass a host object, the results are implementation dependent.

    If you don’t pass a value (or you explicitly pass the primitives undefined or null) a new object that inherits from Object.prototype will be created.

    Otherwise, if you pass any of the remaining primitives (as a Number, String or a Boolean value), a primitive wrapper object will be created (basically “primitive-to-object” type conversion), for example.

    var s = new String("foo"); // a string object wrapper
    typeof s;     // "object"
    s.valueOf();  // "foo"
    

    See this question about primitives and objects: How is a Javascript string not an object?

    In your second snippet, the line:

    var bar = Object.create( foo );
    

    Creates a new object, that inherits from foo, and since it’s a different object, when you assign the properties:

    bar.three = 3;
    bar.one = 100;
    

    Those will be created physically on that separated instance, as you can see, the bar.one property shadows the value contained in foo.

    The object referenced by bar, in fact will contain two own properties (one and three, but since it inherits from foo, the property named two is resolvable through the prototype chain, for example:

    bar.hasOwnProperty('one'); // true, the property is "own"
    bar.hasOwnProperty('two'); // false, the property is "inherited" from foo
    bar.two; // 2, is accessible
    

    Basically, the prototype chain of bar looks like this:

                                                          -----------------
                                               ========> | Object.prototype| ==> null
                                               |          -----------------
    |-------------|     [[Prototype]]     |---------|
    | one:   100  | ====================> |  one: 1 | (shadowed)
    | three: 3    |                       |  two: 2 |
    |-------------|                       |---------|
    
    (== line denotes the prototype chain)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to write this JavaScript code without eval? var typeOfString = eval(typeof + that.modules[modName].varName);
I have some LINQ code that generates a list of strings, like this: var
I have this code that performs an ajax call and loads the results into
Recently I saw the following code that creates a class in javascript: var Model.Foo
I've the following code: var e = someList.GetEnumerator(); var a = new List<Foo>(); var
i am new to JS objects. I have such code var foo = {
I ended up with this code in one of my projects: function foo(type, desc)
I was under the impression that this code #include <windows.h> #include <stdio.h> int WINAPI
I found that this C++ code: vector<int> a; a.push_back(1); a.push_back(2); vector<int>::iterator it = a.begin();
I have the following code: Bear in mind that while this code works on

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.