4 ms·
I keep thinking that the optional chaining operator would also work for functions e.g. this.myOptionalfunction?('hello') were I would expect it would only get c
by wdb 6y ago
I keep thinking that the optional chaining operator would also work for functions e.g. this.myOptionalfunction?('hello') were I would expect it would only get called when it's defined. But that's not possible, I think they missed a nice addition.
Oh looks like the operator is actually '?.' instead of just '?' so guess this.myOptionalFunction?.() could work but that looks really confusing in my eyes.
- Legogris 6y agoI am trying to think of a scenario where this makes sense, but I'm struggling. When do you want to call a function that might exist, and if so, not care about if it exists or not? (Since null or undefined returned from the function call can't be distinguished from the non-presence of the function)
- jlokier 6y agoIt has to be '?.(...)' not '?(...)', because '?(...)' is already part of valid syntax. For example "this.myOptionalfunction?('hello'):('goodbye')".