2

All, Say we have the code like below.

var b ={};

var a=b;
b=null;
if (a==null)
{
    alert('a is null');
}

Before the code running, I had thought the a should be null, because I thought a and b are pointing to same object or they are supposed to be the same address. but it is not . Isn't javascript object reference type like classical language (c++/c#/java)? Or did I miss something important? Thanks.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
  • 1
    it is like java, yes. `a` and `b` are pointing to the same object, when `b` does now point to `null`, then `a` still points to the object. – Bergi Jun 24 '14 at 01:55
  • `b` contained a reference to the same object as `a`. Then you modified it `b` to contain a value of `null` instead, but `a` still points at the original object. – Jeremy Jun 24 '14 at 01:55

6 Answers6

6

In JavaScript, all variables are held -- and passed -- by value.

However, in the case of objects (anything not a primitive) that value is a reference.

var v1, v2;
v1 = {
  someProp: true
}; // Creates an object

v2 = v1; // The object now has two references pointed to it.
v1 = null; // The object now has one reference.
console.log(v2); // See the value of the object.
v2 = null; // No references left to the object. It can now be garbage collected.
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
3

No. In fact, there are no "reference" types in JavaScript1 and there is definitely not Reference semantics for variables. There are two classes of values: Primitives, and Objects. However, this question/problem doesn't deal with the distinction.

Rather the question is due to not understanding variable assignment:

// let X be a singleton object, {}
var b = X;    // the object X is assigned to the variable `b`
              // (this does NOT make a copy)
              // b -> X
var a=b;      // the object that results from evaluating `b` is assigned to `a`
              // however, `a` and `b` are SEPARATE non-related variables
              // (once again, no copies are made)
              // b -> X, a -> X
b=null;       // assigns the value null to `b` - this does NOT AFFECT `a`
              // b -> null, a -> X
b==null       // true, as b -> null
a==null       // false, as a -> X

1 There are mutable objects, and then there is the Reference Specification Type; the RST does not directly apply to the question and it is not related to "reference" types, but it is used to describe l-value behavior of an assignment.

While implementations may use "references" or "pointers" internally, the semantics are entirely defined merely by accepting that an object is itself and that neither an assignment nor use in an expression (such as a function argument) create a copy.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    +1. Some more background: [How to explain object references in ECMAScript terms?](http://stackoverflow.com/q/23554770/1048572) – Bergi Jun 24 '14 at 02:10
  • Well, there is a [*reference specification type*](http://ecma-international.org/ecma-262/5.1/#sec-8.7). ;-) – RobG Jun 24 '14 at 03:30
1

It doesn't work that way in any language I know. JavaScript still uses references to objects, just not to variables.

Assignment changes what the variable contains. When you do b=null;, you don't change the object, you change b. Since a hasn't changed, it still contains the old reference to the original object.

If you did, say:

var b = {};
var a = b;
b.foo = "bar";
if (a.foo === "bar")
{
    alert("foobar");
}

Then the alert would indeed run.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
1

The reason a is not null is because you are assigning it to the value of b BEFORE setting b to null. In Javascript, the variable saves whatever the right hand side is evaluated to when it was assigned. Therefore, if you wanted a to be null, you would do it like this:

var b = {};
b = null;
var a = b;

alert(a) // outputs null
joshboley
  • 1,143
  • 6
  • 10
0

Adding one more - Self Ref - Cyclic Ref in JS

var x = {name:'myName'}
var y=x
x['y'] = y

Object {name: "myName", y: Object}

enter image description here

Harpreet Singh
  • 2,651
  • 21
  • 31
0

As Bergi said, It is like java or C#!

I tested it in the C#.

                TestClass b = new TestClass();
                TestClass a = b;
                b = null;//doesn't change the object , 
//only change the variable b. the object is remained.
                if (a == null)
                {
                    Console.WriteLine("a is null");//not hit here
                }
                Console.ReadLine();

Thanks you all. It really makes me understand it better.

Joe.wang
  • 11,537
  • 25
  • 103
  • 180