The only way to actually make that work is:
var adam = {
name: "Adam"
};
var terah = {
name: "Terah",
age: 32,
height: 66,
weight: 125,
hairColor: "brown"
};
adam.spouse = terah;
terah.spouse = adam;
It's not an error to reference the variable "terah" in the object literal initializing "adam" (thanks to the hoisting of var
declarations), but at the point the code is evaluated the value of "terah" will be undefined
. The fact that it's later given a value doesn't matter.
(The object literal for "terah" could refer to the "spouse" property of "adam", but I split that out for clarity.)
Note that a circular reference like this won't be serializable as JSON. It might not throw an exception, but there's no way to represent a cycle like that in JSON.