mdn / sprints

Archived: MDN Web Docs issues are tracked in the content repository.
https://github.com/mdn/content
Creative Commons Zero v1.0 Universal
150 stars 142 forks source link

Inaccurate Example of Optional Chaining #3930

Closed Dectom closed 3 years ago

Dectom commented 3 years ago

Request type

Details

On the Optional Chaining Page under the "Stacking the optional chaining operator" Section

At the bottom of the example it has the following two lines

// … this also works with optional chaining function call
let duration = vacations.trip?.getTime?.();

Vacations isn't referenced anywhere else on the page neither is trip or getTime so this wouldn't work in the example given and the optional chaining would return a TypeError for non-existent root element?

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

filipprosovsky commented 3 years ago

You are right, it is confusing to have different naming in same code block which does not refer to anything. This can be rewritten to let customerName = customer.name?.getName?.(); console.log(customerName) which results in undefined as expected and we keep the same example as defined object above. @chrisdavidmills please assign and I'll rewrite it. Thanks

chrisdavidmills commented 3 years ago

Sounds good @prosovskyf , assigned!

filipprosovsky commented 3 years ago

@chrisdavidmills please review

chrisdavidmills commented 3 years ago

@prosovskyf looks good to me; thanks!