0

I can't figure this out...

I have two simple objects defined:

 var adam = {
  name: "Adam",
  spouse: terah

}

var terah = {
  name: "Terah",
  age: 32,
  height: 66,
  weight: 125,
  hairColor: "brown",
  spouse: adam
}

The only property I'm concerned with is the spouse property. When I test:

console.log(terah.spouse.spouse);
> Object {name: "Terah", age: 32, height: 66, weight: 125, hairColor: "brown"…}

I get the object I want here. But when I make it a conditional

terah.spouse.spouse === terah;
>false

I get false... Why is this? It seems to be pointing to the same object. Even when I call

terah.spouse.spouse.name === "Terah"
>true

I get true there. Why do I get false with the object conditional? Thanks.`

10
  • 2
    The way your objects are set up, adam.spouse will be undefined. I assume this is not your actual code. As such, you should provide actual code that demonstrates the issue. Commented Dec 30, 2013 at 17:19
  • How can you define adam .. {spouse: terah} before terah is defined? or how will you define terah {spouse: adam} before adam is defined? this gives me a headache... Commented Dec 30, 2013 at 17:21
  • In your example, terah.spouse.spouse will result undefined Commented Dec 30, 2013 at 17:21
  • @Darren When I try terah.spouse.spouse.name === "Terah" all I get is TypeError: 'undefined' is not an object (evaluating 'terah.spouse.spouse.name') Commented Dec 30, 2013 at 17:23
  • 1
    @DarrenDahl It's usually a good idea to doublecheck your code samples in a clean tab before posting ;) Commented Dec 30, 2013 at 17:33

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Open the Object view and make sure they're actually the same... running your code (a few times... because of the weird recursion) gives me

>terah.spouse.spouse
age: 32
hairColor: "brown"
height: 66
name: "Terah"
spouse: Object
name: "Adam"
spouse: undefined //undefined
__proto__: Object
weight: 125
__proto__: Object
>terah
age: 32
hairColor: "brown"
height: 66
name: "Terah"
spouse: Object
name: "Adam"
spouse: Object //not undefined! so they ARE different!
__proto__: Object
weight: 125
__proto__: Object

See? two object were created. the real terah and an earlier 'version' of terah.

Have you tried just setting adam.spouse = terah?

Comments

1

At the time that you define adam, the object for terah doesn't exist, so at that time, terah.spouse is undefined. If you were to define Adam's spouse after defining Terah, you would get the result you are looking for:

var adam = {
    name: "Adam",
}

var terah = {
    name: "Terah",
    age: 32,
    height: 66,
    weight: 125,
    hairColor: "brown",
    spouse: adam
}

adam.spouse = terah

console.log(terah.spouse.spouse === terah) //true

4 Comments

That doesn't explain the output in the rest of the question. Clearly adam.spouse is not undefined.
In the original question, adam.spouse is set to an undefined variable (terah) so it is undefined. I'm not sure how the OP got the first output. See here, undefined
Right, that's my point. Since OP is getting an object at adam.spouse, then there's something additional that's missing in the question. Otherwise it wouldn't log an object, and terah.spouse.spouse.name would throw an error.
OK, but 'something missing in the question' is surely down to the OP? I've just answered the question how I can best, trying to interpret what the OP is asking.

Your Answer

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.