Cofey
Cofey

Reputation: 11404

Need help with dot notation

Here's an example of my object:

var fruit = {
    apple: {
    }
}

var apple = this.rel;

Will someone please tell me why this works:

fruit[apple]

and this doesn't?

fruit.apple

Upvotes: 1

Views: 116

Answers (1)

kennytm
kennytm

Reputation: 523264

In Javascript foo.bar is equivalent to foo["bar"], not foo[bar].

Therefore, fruit.type will become fruit["type"], but there isn't a type: field in the fruit object, so fruit.type returns undefined.

Upvotes: 5

Related Questions