Accessing an object with a square object syntax | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Accessing an object with a square object syntax

var roomsFree = hotel['checkAvailability'](); The checkAvailability is a property of an object that contains a function. So why does the () on the above statement outside the square bracket syntax and the single quotes? Shouldn't the () be with the 'checkAvailability' property?

20th Aug 2016, 9:04 PM
Genesis
1 Answer
+ 1
Think about it this way. hotel.checkAvailability is equivalent to hotel['checkAvailibility'] in that it is a reference to the function. To execute it, you add the parens. You are executing the reference, so you append the parens after the reference has been defined. The 'checkAvailability' is simply the key youre looking up, it wouldnt make sense to add the invocation parens to they key.
21st Aug 2016, 5:27 PM
LFP6
LFP6 - avatar