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

javascriptの参照と値

 javascriptの参照と値

javascriptの参照渡しと値渡しについて

Transcript

  1. JAVASCRIPT
    REFERENCES & VALUES

    View full-size slide

  2. Name
    @brn (⻘野健利)
    Occupation
    フロントエンドエンジニア・ネイティブエンジニア
    Company
    Cyberagent アドテクスタジオ AI Messenger
    OSS
    Contributor of V8
    About
    http://info.b6n.ch

    View full-size slide

  3. References and Values
    ࢀর ஋

    View full-size slide

  4. References and Values


    ࢀর
    ࢀর

    View full-size slide

  5. References and Values
    ࢀর͸ؒ઀తʹΦϒδΣΫτΛมߋ͢Δ
    ஋͸௚઀ͦͷΦϒδΣΫτΛมߋ͢Δ

    View full-size slide

  6. References or Values?
    KBWBTDSJQU͸ࢀরͱ஋ͷ྆ํΛ࢖͏
    ΦϒδΣΫτຖʹͲ͏ѻΘΕΔ͔ܾ·͍ͬͯΔ

    View full-size slide

  7. Javascript object traits
    ࢀর
    "MMWBMVFTFYUFOET0CKFDU

    TUSJOH OVNCFS CPPMFBO

    View full-size slide

  8. Pass by reference/value
    const A =
    const B =
    ࢀর

    A =
    B = ஋

    View full-size slide

  9. Pass by reference
    const A = {};
    const B = A;
    B.x = 1;
    console.log(A);
    // { x: 1 }

    View full-size slide

  10. Pass by value
    const A = 1;
    const B = A;
    B++;
    console.log(A);
    // 1

    View full-size slide

  11. Until here…
    ͱɺ͜͜·Ͱ͕Α͘ݴΘΕ͍ͯΔ
    ࠓճ͸΋͏ͪΐ͍ਂ۷Γͯ͠Έ͍ͨ

    View full-size slide

  12. Mutable, Immutable
    KBWBTDSJQUʹ͸
    มߋՄೳͳ஋ͱมߋෆՄೳͳ஋͕͋Δ
    TUSJOH OVNCFS CPPMFBO͸มߋͰ͖ͳ͍

    View full-size slide

  13. Special cases
    OVNCFSͱCPPMFBO͸஋౉͠Ͱ͋Δ͜ͱ͕ଟ͍
    ͨͩ͠TUSJOH͸৭ʑͳ޻෉͕͋Δέʔε͕ଟ͍
    7Ͱ͸TUSJOH͸ࢀরͱͳ͍ͬͯΔ

    View full-size slide

  14. String behave like value
    let A = 'a';
    let B = A;
    A += 'b' // Create new instance and
    assign to self.
    console.log(A);
    // 'ab';
    console.log(B);
    // 'a;

    View full-size slide

  15. Immutablity make reference to value
    *NNVUBCMFͳจࣈྻ͕஋ͷΑ͏ʹৼΔ෣͏͕
    ࣮ࡍʹ͸͢΂ͯࢀরΛ౉͍ͯ͠Δ

    View full-size slide

  16. What is reference?
    ࢀরͱ͸ͳΜͩΖ͏

    View full-size slide

  17. Pointer
    ΦϒδΣΫτ͸ੜ੒͞ΕΔͱώʔϓྖҬͱ͍͏ϝ
    ϞϦʹׂΓ౰ͯΒΕΔ
    ͦͷΦϒδΣΫτΛૢ࡞͢Δ৔߹͸ͦͷϝϞϦྖ
    ҬΛࢦࣔ͢͠ΞυϨεΛඞཁͱ͢Δ
    ͜Ε͕ϙΠϯλͱݺ͹ΕΔ΋ͷ

    View full-size slide

  18. Pointer is value
    KTΤϯδϯΛ࣮૷͍ͯ͠Δ$$ͷੈքͰ͸
    ϙΠϯλͦͷ΋ͷ΋·ͨ஋Ͱ͋Δ
    ͦͷͨΊϙΠϯλͷϙΠϯλΛ࡞ͬͨΓ΋Ͱ͖Δ

    View full-size slide

  19. Reference is value
    ͔͠͠KBWBTDSJQUͷੈքͰ͸ࢀরͷࢀর͸࡞Εͳ͍
    ͭ·Γࢀর͸ৗʹ஋౉͠ͱͳΔ

    View full-size slide

  20. pass by copy reference
    ͭ·ΓKBWBTDSJQUͰ͸
    ࢀর ϙΠϯλ
    Λ஋౉͍ͯ͠͠Δ
    ࢀরͷ஋౉͠ͱͳ͍ͬͯΔ

    View full-size slide

  21. Appendix
    7಺෦ͷจࣈྻ࣮૷Λ؆୯ʹ

    View full-size slide

  22. String in V8
    7Ͱ͸จࣈྻ͕ҎԼͷΫϥεʹ෼͔Ε͍ͯΔ
    4FR0OF#ZUF4USJOH
    4FR5XP#ZUF4USJOH
    $POT4USJOH
    5IJO4USJOH
    4MJDFE4USJOH

    View full-size slide

  23. String in V8
    ಛʹTVCTUSJOHΛߦͬͨΓͯ͠
    ৽ͨʹจࣈྻΠϯελϯεΛ࡞੒͢Δ৔߹
    3PQFͱ͍͏ΞϧΰϦζϜΛར༻ͨ͠
    $POT4USJOH͕ར༻͞ΕΔ

    View full-size slide

  24. Rope
    จࣈྻͷ݁߹͕ߴ଎0 MPHO

    ෼ׂ΋ߴ଎0 MPHO

    औΓग़͠0 MPHO

    View full-size slide

  25. Rope
    5PEBZJTXFBSFKBWBTDSJQUFS`T
    5PEBZJT XFBSFKBWBTDSJQUFS`T
    3005

    View full-size slide

  26. Rope
    5PEBZJT
    3005
    8FBSF KBWBTDSJQUFS`T
    53&&

    View full-size slide

  27. Summary
    ࢀরͱ஋͔Β3PQF·ͰோΊͯΈ·ͨ͠
    ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide