<html>
<script language="JavaScript">
var object1 = {value: 10};
var object2 = object1;
var object3 = {value: 10};
alert(object1 == object2);
alert(object1 == object3);
object1.value = 15;
alert(object2.value);
alert(object3.value);
</script>
</html>
object1
and object2
are two variables grasping the same value. There is only one actual object, which is why changing object1
also changes the value of object2
. The variable object3
points to another object, which initially contains the same properties as object1
, but lives a separate life.
JavaScript's ==
operator, when comparing objects, will only return true
if both values given to it are the precise same value. Comparing different objects with identical contents will give false
.