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.
6 Answers 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.
Comments
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.
4 Comments
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.
Comments
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
Comments
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
Comments
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.
aandbare pointing to the same object, whenbdoes now point tonull, thenastill points to the object.bcontained a reference to the same object asa. Then you modified itbto contain a value ofnullinstead, butastill points at the original object.