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
62.5k15 gold badges159 silver badges234 bronze badges
asked Jun 24, 2014 at 1:51
2
  • 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. Commented Jun 24, 2014 at 1: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. Commented Jun 24, 2014 at 1:55

6 Answers 6

7

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.
answered Jun 24, 2014 at 1:55
Sign up to request clarification or add additional context in comments.

Comments

2

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.

answered Jun 24, 2014 at 2:00

4 Comments

Well, there is a reference specification type. ;-)
I love how this is the result that Google shows, but it's quite a misleading first paragraph, because obviously objects are reference types otherwise a function mutating an object wouldn't mutate the caller's "version" of that object, too.
When discussing some of the technicalities, it might be a good idea not to use the word "class" here. Traditionally, classes refer to objects.
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.

answered Jun 24, 2014 at 1:56

Comments

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
answered Jun 24, 2014 at 1:57

Comments

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

answered Jun 24, 2014 at 2:17

Comments

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.

answered Jun 24, 2014 at 2:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.