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

第7回みゆっき☆Think 本気で学ぶ JavaScript

第7回みゆっき☆Think 本気で学ぶ JavaScript

2011/09 にニコ生で放送した「みゆっき☆Think」の資料
http://www.nicovideo.jp/watch/1315569331

Takuya Fujimura

September 02, 2011
Tweet

More Decks by Takuya Fujimura

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ౻ଜ ୓໵ (id: tlync) • ϞόΠϧࣄۀຊ෦ اը։ൃ෦
 •

    εϚʔτϑΥϯ޲͚ΞϓϦ։ൃ
 • υϫϯΰ৽ࢀऀ … ೖࣾ3ϲ݄໨
  2. “JavaScript is the only language that people think they can

    program without actually learning it.”  ! - Douglas Crockford
  3. 1.Object Λཧղ͢Δ
 ɾ΄ͱΜͲ͢΂ͯ͸ Object
 ɾ͢΂ͯͷ Object ͸ࣗ਎ͷݪܕΛ஌͍ͬͯΔ ࠓ೔ֶͿ͜ͱ 2. Function

    Λཧղ͢Δ
 ɾFunction ͷ 2 ͭͷإ
 ɾܧঝͷ࢓૊Έ - prototype ͱ __proto__
  4. 1.Object Λཧղ͢Δ
 ɾ΄ͱΜͲ͢΂ͯ͸ Object
 ɾ͢΂ͯͷ Object ͸ࣗ਎ͷݪܕΛ஌͍ͬͯΔ ࠓ೔ֶͿ͜ͱ 2. Function

    Λཧղ͢Δ
 ɾFunction ͷ 2 ͭͷإ
 ɾܧঝͷ࢓૊Έ - prototype ͱ __proto__ 3. Closure Λཧղ͢Δ
 ɾ໊લղܾͷ࢓૊Έ - είʔϓνΣΠϯ
 ɾ࣮͸͍ͭ΋਎ۙʹ
  5. • จ๏ͷ͸ͳ͠
 … ͍͍ͩͨΈΜͳ஌ͬͯΔΑͶ
 … ஌Βͳ͚Ε͹ Mozilla ͷαΠτͱ͔ ࠓ೔΍Βͳ͍ࣄ •ΫϥΠΞϯταΠυεΫϦϓτͷ͸ͳ͠


    … DOM ͱ͔ Ajax ͱ͔
 … ͦΕ IE ͩͱ(ry •ECMA Script 5 ͷ͸ͳ͠
 … ͕࣌ؒͳ͔ͬͨͷͰػձ͕͋Ε͹·ͨ
 … ิ଍ͱͯ͠࿩ͤΔͱ͜Ζ͸࿩͠·͢
  6. • 5෼ͰֶͿ JavaScript ͷྺ࢙ • Object ͔Βཧղ͢Δ JavaScript • Function

    ͷ2ͭͷإ • Closure ͱείʔϓ • ࣭໰λΠϜ Agenda
  7. ES 5 •݁ہ ES 3.1 ϕʔεʹ •৽͍͠จ๏͸௥Ճ͠ͳ͍ •strict Ϟʔυ •JSON

    αϙʔτ •ϓϩύςΟΞΫηοα·ΘΓ ※࠷ۙͷϒϥ΢β͸΄΅ରԠ͍ͯ͠Δ http://kangax.github.com/es5-compat-table/
  8. ES Harmony •Class ͳͲ ES 4 ͷ࢓༷͕Ұ෦ΧϜόοΫ? •2೥ޙ͘Β͍? •ৄࡉ͸ wiki

    ࢀর http://wiki.ecmascript.org/doku.php?id=harmony:harmony
  9. Object ܕ
 Object
 Function
 Array
 String
 Number
 Boolean
 RegExp
 Date


    etc...
 ͨͩɺ΄ͱΜͲ͸ Object null
 undefined ※ࣗಈม׵ string
 number
 boolean جຊσʔλܕ
  10. • ۭͷ Object ࡞੒͸ {} ͔ new Object()
 ɾ{} ͷදه͸ཪͰ

    new Object() ͕ݺ͹Ε͍ͯΔ 
 ɾυοτ͔தׅހͰϓϩύςΟ΁ΞΫηεͰ͖Δ
 ɾޙऀ͸ Java ʹ͓͚ΔϦϑϨΫγϣϯΈ͍ͨʹ࢖͑Δ ෼͔ͬͨ͜ͱ •Object ܕ͸ಈతʹ௥Ճ΋࡟আͳΜͰ΋Ͱ͖Δ
 ɾجຊσʔλܕ͚ͩͰͳؔ͘਺΋ Object ࣗ਎΋ؚΊΒ
 ΕΔ
 ɾҰ෦ɺॲཧܥʹΑͬͯ͸ίΞͱͳΔΦϒδΣΫτ͸ 
 ແཧͳ΋ͷ΋͋Δ
  11. ϓϩτλΠϓ(__proto__) miyukki name miyukki age 18 __proto__ toString ಺෦࣮૷ ଞ…

    ಺෦࣮૷ __proto__ OVMM Object.prototype ※ __proto__ ͷϓϩύςΟ໊͸ॲཧܥʹΑͬͯҟͳΔ ྫ͑͹ IE Ͱ͸࢖͑ͳ͍ ·ͨ ES 5 Ͱ͸ඇਪ঑
  12. • ΄ͱΜͲ͢΂ͯ͸ Object
 ɾجຊσʔλܕɺnull, undefined Ҏ֎͸ Object
 ɾجຊσʔλܕ͸҉໧తʹ Object ͱͯ͠ৼΔ·͏

    Object ·ͱΊ •Object ͸ϓϩύςΟͷೖΕ΋ͷ (= ϋογϡ)
 ɾ໊લ(จࣈྻ)ɺ஋(ͳΜͰ΋)ͷϖΞʔͷೖΕ΋ͷ
 ɾಈతʹϓϩύςΟΛ௥Ճɺ࡟আͰ͖Δ
  13. • ΄ͱΜͲ͢΂ͯ͸ Object
 ɾجຊσʔλܕɺnull, undefined Ҏ֎͸ Object
 ɾجຊσʔλܕ͸҉໧తʹ Object ͱͯ͠ৼΔ·͏

    Object ·ͱΊ •Object ͸ϓϩύςΟͷೖΕ΋ͷ (= ϋογϡ)
 ɾ໊લ(จࣈྻ)ɺ஋(ͳΜͰ΋)ͷϖΞʔͷೖΕ΋ͷ
 ɾಈతʹϓϩύςΟΛ௥Ճɺ࡟আͰ͖Δ •͢΂ͯͷ Object ͸ࣗ਎ͷݪܕ(__proto__)Λ࣋ͭ
 ɾࠓ͸ͱΓ͋͑ͣͳΜͱͳ͘ͷཧղͰ OK
 

  14. Object ܕ
 typeof Object => 'function'
 typeof Function => 'function'


    typeof Array => 'function'
 typeof String => 'function'
 typeof Number => 'function'
 typeof Boolean => 'function'
 typeof RegExp => 'function'
 typeof Date => 'function'
 ...

  15. Object ܕ
 Object instanceof Function=> true
 Function instanceof Function =>

    true
 Array instanceof Function => true
 String instanceof Function => true
 Number instanceof Function => true
 Boolean instanceof Function => true
 RegExp instanceof Function => true
 Date instanceof Function => true
 ...

  16. 1.ۭͷ Object Λੜ੒ 2.಺෦తʹ [[Class]] ͱ͍͏छผΛද͢ϓϩύ ςΟΛ͕ηοτ͞ΕΔ 3.prototype ΦϒδΣΫτ͕ɺ৽͍͠ Object

    ͷ __proto__ ϓϩύςΟʹηοτ͞ΕΔ 4.࡞੒ͨ͠ Object Λ this ͱͯ͠ίϯετϥΫ λΛ࣮ߦ 5.ઃఆ͞Εͨ৽͍͠ Object Λฦ͢ ίϯετϥΫλͰߦΘΕΔ͜ͱ
  17. Person __proto__ Function.prototype sayHello <function> Function __proto__ Object.prototype ૊ΈࠐΈؔ਺… <function>

    Object __proto__ OVMM ૊ΈࠐΈؔ਺… <function> __proto__ Λḷͬͯ
 ϓϩύςΟ͕୳͞ΕΔ var miyukki = new Person('miyukki'); miyukki.toString(); // Object.prototype ͷϝιου
  18. • Function ͸ new ΩʔϫʔυͱηοτͰίϯε τϥΫλͱͯ͠ػೳ͢Δ - ίϯετϥΫλؔ਺ʹఆٛ͞Εͨ prototype ΦϒδΣ

    Ϋτ͕ɺ৽͘͠࡞੒͞ΕΔΦϒδΣΫτͷϓϩτλΠ ϓ(__proto__)ͱͯ͠ηοτ͞ΕΔ - ͜͏ͯ͠ Prototype Chain ͕࡞ΒΕΔ Function ·ͱΊ
  19. • Function ͸ new ΩʔϫʔυͱηοτͰίϯε τϥΫλͱͯ͠ػೳ͢Δ - ίϯετϥΫλؔ਺ʹఆٛ͞Εͨ prototype ΦϒδΣ

    Ϋτ͕ɺ৽͘͠࡞੒͞ΕΔΦϒδΣΫτͷϓϩτλΠ ϓ(__proto__)ͱͯ͠ηοτ͞ΕΔ - ͜͏ͯ͠ Prototype Chain ͕࡞ΒΕΔ Function ·ͱΊ •ϓϩτλΠϓʹ͸ 2 ͭͷ֓೦͕͋Δ - Function ʹఆٛ͢Δ prototype ͸ɺͦͷίϯετϥΫλ ͔Βੜ੒͞ΕΔΦϒδΣΫτୡͰڞ༗͍ͨ͠ΦϒδΣΫτ Λఆٛ͢Δҝʹ࢖͍ɺ - Object ʹඥ෇͘ __proto__ ͸ɺͦͷڞ༗ΦϒδΣΫτ Λࢀর͢Δ(ḷΔ)ҝʹ࢖ΘΕΔ
  20. • ES5 Ͱ͸ new Λ࢖Θͳ͍ Object.create ϝ ιου͕༻ҙ͞Ε͍ͯΔɻ • ES3

    Ͱ͸ग़དྷͳ͍ null Λ prototype ͱͨ͠ɺ ຊ౰ͷۭͷ Object Λ࡞Δ͜ͱ΋Ͱ͖Δɻ ஫ҙॻ͖
  21. <script type="text/javascript"> // ؔ਺֎Ͱͷએݴ => άϩʔόϧείʔϓ => άϩʔόϧม਺
 var count

    = 10; console.log(count); //=> 10 ! ! var hello = function(){ // ؔ਺಺Ͱͷએݴ => ϩʔΧϧείʔϓ => ϩʔΧϧม਺ var hoge = 'hoge'; ! // ϩʔΧϧม਺ => ͜ͷؔ਺಺͔ΒͷΞΫηεՄೳ console.log(hoge); //=> hoge ! // άϩʔόϧม਺ => Ͳ͔͜ΒͰ΋ΞΫηεՄೳ console.log(count); //=> 10 }
 </script>
  22. <script type="text/javascript"> // ؔ਺֎Ͱͷએݴ => άϩʔόϧείʔϓ => άϩʔόϧม਺
 var count

    = 10; console.log(count); //=> 10 ! ! var hello = function(){ // ؔ਺಺Ͱͷએݴ => ϩʔΧϧείʔϓ => ϩʔΧϧม਺ var hoge = 'hoge'; ! // ϩʔΧϧม਺ => ͜ͷؔ਺಺͔ΒͷΞΫηεՄೳ console.log(hoge); //=> hoge ! // άϩʔόϧม਺ => Ͳ͔͜ΒͰ΋ΞΫηεՄೳ console.log(count); //=> 10 }
 </script> ؔ਺಺ => ϩʔΧϧείʔϓ ؔ਺֎ => άϩʔόϧείʔϓ
  23. ྫ͑͹͜ͷ༷ͳ࣌ <script type="text/javascript"> var hoge = ‘hoge’; ! function add(n1,

    n2){ var sum = n1 + n2; return sum; } ! add(10, 20);
 </script>
  24. ྫ͑͹͜ͷ༷ͳ࣌ <script type="text/javascript"> var hoge = ‘hoge’; ! function add(n1,

    n2){ var sum = n1 + n2; return sum; } ! add(10, 20);
 </script> BEE n1 10 n2 20 sum undefined arguments {0: 10, 1: 20,...}
  25. ྫ͑͹͜ͷ༷ͳ࣌ <script type="text/javascript"> var hoge = ‘hoge’; ! function add(n1,

    n2){ var sum = n1 + n2; return sum; } ! add(10, 20);
 </script> BEE n1 10 n2 20 sum undefined arguments {0: 10, 1: 20,...} HMPCBM hoge 'hoge' add <function> ଞ૊ΈࠐΈͷม਺
  26. BEE n1 10 n2 20 sum undefined arguments {0: 10,

    1: 20,...} HMPCBM hoge 'hoge' add <function> ଞ૊ΈࠐΈͷม਺ ͜ͷॱ൪Ͱม਺͕୳͞ΕΔ
  27. Closure + είʔϓ ·ͱΊ •جຊతͳείʔϓ͸2छྨ - άϩʔόϧείʔϓͱϩʔΧϧείʔϓ - ϩʔΧϧείʔϓ಺Ͱ͸Ͳ͜Ͱએݴͯ͠΋ಉ͡ =

    
 ϒϩοΫείʔϓ͕ͳ͍ •ม਺ͷ໊લղܾͷ࢓૊Έ - ؔ਺ͷ࣮ߦίϯςΩετຖʹม਺Λอ࣋͢ΔΦϒδΣ΢τ ͕ੜ੒͞ΕΔɻͪͳΈʹ͜ΕΛ Activation ΦϒδΣΫ τ ͱݺͿɻάϩʔόϧͷྫ͸ ม਺ΦϒδΣΫτ ͱݺͿ - ม਺͸ͦΕΒͷΦϒδΣΫτΛอ࣋ͨ͠ είʔϓνΣΠ ϯ ḷͬͯղܾ͞ΕΔɻ

  28. • Closure ͱ͸ɺείʔϓΛอ࣋ͨؔ͠਺ͱม਺ ͷ·ͱ·Γ - ͋Δؔ਺ʹ͓͚Δ֎ଆͷྖҬΛ ϨΩγΧϧείʔϓ ͱݺͿ - jQuery

    ࢖ͬͯΔͱී௨ʹ࢖ͬͯΔ - ࢖͍ಓ͸ແݶେɻෳࡶͳॲཧ͕ޮ཰తʹɺΤϨΨϯτ ʹॻ͚ͨΓ Closure + είʔϓ ·ͱΊ
  29. 1.Object ɹ
 ɾ΄ͱΜͲ͢΂ͯ͸ Objectɻ
 ɾϓϩύςΟͷೖΕ΋ͷɻ͔͠΋ಈతɺࣗ༝ࣗࡏɻ ࠓ೔ֶΜͩ͜ͱ 2. Functionɹ
 ɾFunction ͷ΋͏ͻͱͭͷإ͸ίϯετϥΫλ


    ɾ2ͭͷϓϩτλΠϓͱϓϩτλΠϓνΣʔϯͷ࿩͠ 3. Closure ɹ
 ɾείʔϓ͸2ͭ͋ΔɻείʔϓνΣʔϯͷ࿩͠
 ɾ࣮͸͍ͭ΋਎ۙʹ