Topic: Objects SubTopic: Accessing Properties using Dot and Bracket Notation:
// object
let spaceship = {
'Fuel Type': 'Turbo Fuel',
'Active Duty': true,
homePlanet: 'Earth',
numCrew: 5
};
// writing function which access object properties using dot notation
let returnAnyProp1 = (objectName1,propName1) => objectName1.propName1;
console.log(returnAnyProp1(spaceship, 'homePlanet'));
// Returns undefined
// directly accessing
console.log(spaceship.'homePlanet')
// returns error: Unexpected String.
My question here is that when we are calling the function, it is returning
spaceship.'homePlanet'
. Right?
So my question is:
- Why is it searching for
propName1
exactly and not the argument we are passing? - Even if let's say it is searching for exactly
propName1
then still it would meanspaceship.'propName1'
. It should still return an error and not undefined right?
PS: I know we can use bracket notation here to solve this but I want to know what exactly am I missing in this concept that I am not understanding this.