Upgrade to Pro — share decks privately, control downloads, hide ads and more …

null or undefined

Susisu
August 24, 2024

null or undefined

Susisu

August 24, 2024
Tweet

More Decks by Susisu

Other Decks in Programming

Transcript

  1. ݱ࣮ • લఏ: "JavaScript" ͸͍ͭ͘΋ͷඪ४࢓༷͔Β੒Γཱ͍ͬͯΔ • ECMAScript ͷݴޠ࢓༷Ͱ͸ද޲͖͸΄΅ unde fi

    ned ͕࢖ΘΕΔ • ͨͩ͠ਖ਼نදݱ, JSON, ϓϩτλΠϓͷΈྫ֎తʹ null ͕ొ৔͢Δ • Web ؔ࿈࢓༷ (DOM, Fetch ͳͲ) Ͱ͸׳ྫతʹ null ͕࢖ΘΕΔ͜ͱ͕ଟ͍ • ҙຯతͳ࢖͍෼͚ΑΓ͸, ΄΅࢓༷͝ͱʹҰ؏ͯ͠ͲͪΒ͔Λ࢖͏
  2. > const x = new Map([["foo", "hello"]]); > x.get("foo"); "hello"

    > x.get("bar"); undefined ECMAScript ͷݴޠ࢓༷Ͱఆٛ
  3. > const x = new Headers([["foo", "hello"]]); > x.get("foo"); "hello"

    > x.get("bar"); null Fetch Standard Ͱఆٛ
  4. > const xs = ["A", "B", "C"]; > xs.find((x) =>

    x === "A"); "A" > xs.find((x) => x === "Z"); ???
  5. > const xs = ["A", "B", "C"]; > xs.find((x) =>

    x === "A"); "A" > xs.find((x) => x === "Z"); undefined ECMAScript ͷݴޠ࢓༷Ͱఆٛ
  6. > const x = "ABC"; > x.match(/A/); ["A", ...] >

    x.match(/Z/); null ECMAScript ͷݴޠ࢓༷Ͱఆٛ ਖ਼نදݱ͸ྫ֎తʹ null
  7. ·ͱΊ • null ͱ unde fi ned ͸࢓༷͝ͱʹ֓ͶҰ؏ͨ͠࢖ΘΕํΛ͍ͯ͠Δ • ECMAScript

    ͷݴޠ࢓༷Ͱ͸ද޲͖͸΄΅ unde fi ned ͕࢖ΘΕΔ • Web ؔ࿈࢓༷Ͱ͸׳ྫతʹ null ͕࢖ΘΕΔ͜ͱ͕ଟ͍ • ҙຯతʹ࢖͍෼͚ΒΕ͍ͯΔ͜ͱ͸΄΅ͳͦ͞͏
  8. ࢖͍෼͚ͷώϯτ • جຊ͸ unde fi ned Λ࢖͏ͷ͕͓͢͢Ί • ݴޠ૊ΈࠐΈͷػೳͱฒ΂ͯҧ࿨ײ͕ग़ʹ͍͘ •

    Web ͷ API ΍, null Λ࢖͏ϥΠϒϥϦʹڧؔ͘࿈͢Δ෦෼ͳΒ
 null Λ࢖ͬͯ౷ҰײΛग़͢ͷ΋ΞϦ • ͳΜʹͤΑҰ؏ͨ͠࢖͍ํ͕͞Ε͍ͯΔͷ͕Θ͔Γ΍͍ͩ͢Ζ͏
  9. ͳΜͰ͜ͷ࿩Λ? • ECMAScript ͷݴޠ࢓༷ʹର͢Δ proposal Ͱ null ͕࢖ΘΕ͍͔ͯͨΒ • https://github.com/arthur

    fi orette/proposal-safe-assignment-operator • > The result should conform to the format [error, null | undefined] or [null, data]. • ͔֬ʹҙຯతʹ͸ؒҧ͍ͬͯͳ͍͕, ࢓༷ͷҰ؏ੑతʹ͸Ͳ͏?