0

A very strange behavior is happening..

This works...

https://jsfiddle.net/ymgzrped/

But this below version does not...

https://jsfiddle.net/ymgzrped/1/

I changed...

 function fillInAddress()

to

 function fillInAddress(mapcomponents) 

Used like this (working)

autocomplete.addListener('place_changed', fillInAddress)

and this (not working)

autocomplete.addListener('place_changed', fillInAddress(new_mapcomponents))

For some reason, when I pass a parameter in, I get an error:

Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'place.address_components')
fillInAddress — _display:176
initAutocomplete — _display:162
(anonymous function) — js:145:280
(anonymous function) — js:145:119
promiseReactionJob
https://jsfiddle.net/js/makeStreamConfig.js.map

It seems to be some sort of scoping error with the code when I pass a parameter, but not when I do not.

What would cause this?

Thank you

Phil
  • 157,677
  • 23
  • 242
  • 245
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • 2
    The difference is `autocomplete.addListener('place_changed', fillInAddress)` passes `fillInAddress` as a function reference whereas `autocomplete.addListener('place_changed', fillInAddress(new_mapcomponents))` attempts to execute `fillInAddress` immediately – Phil Oct 09 '19 at 04:09
  • @phil solution: https://jsfiddle.net/bawqyd3n/. thank you – user2012677 Oct 09 '19 at 10:47
  • @Phil, In what example use case would you just pass the function, with no execution? Can you show an example? – user2012677 Oct 09 '19 at 12:19
  • I'm sorry, I don't understand the question – Phil Oct 09 '19 at 20:08
  • Why would someone just pass a function, without wrapping it in an anonymous function to execute it? – user2012677 Oct 09 '19 at 20:15
  • If you mean, why pass `functionReference` instead of `function() { functionReference() }`, the answer would be because they both do exactly the same thing only the former doesn't introduce any extra objects – Phil Oct 10 '19 at 00:04

0 Answers0