SlideShare a Scribd company logo
JavaScript
JavaScript
JavaScript
みゆっき☆Think#7 「本気で学ぶJavascript」
•   (id: tlync)

•
•
•         …       3
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
“JavaScript is the only language that
people think they can program without
         actually learning it.”

                     - Douglas Crockford
k?
“JavaScript
                          HAHA” (   )

              -




                  (   )
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
…
jQuery
        ≠
JavaScript
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
JavaScript
みゆっき☆Think#7 「本気で学ぶJavascript」
3
みゆっき☆Think#7 「本気で学ぶJavascript」
1. Object
                     Object
            Object
1. Object
                       Object
              Object

2. Function
     Function    2
                  - prototype   __proto__
1. Object
                       Object
              Object

2. Function
     Function    2
                  - prototype   __proto__

3. Closure
                       -
みゆっき☆Think#7 「本気で学ぶJavascript」
•
    …
    …   Mozilla
•
    …
    …         Mozilla

•
    … DOM    Ajax
    …   IE   (ry
•
    …
    …          Mozilla

•
    … DOM    Ajax
    …   IE    (ry

• ECMA Script 5
    …
    …
Agenda

•5          JavaScript

•Object                  JavaScript

•Function    2

•Closure
•
5   JavaScript
5         JavaScript
    ※ :
@bad_at_math


http://www.slideshare.net/badatmath/js-shistory
1995   …
Netscape Navigator 2.0
JavaScript
(   LiveScript       )
Mozilla CTO
Java   (   )
•Java Applet
•
…   …
!
Netscape Navigator
                 !
Mozilla CTO




       http://brendaneich.com/tag/history/
Scheme              …




         http://brendaneich.com/tag/history/
!?        !
※SICP




         http://brendaneich.com/tag/history/
http://brendaneich.com/tag/history/
Java




       http://brendaneich.com/tag/history/
!? Scheme        …?
  ※SICP




            http://brendaneich.com/tag/history/
•     …

•Java                ……

•Gabage Collection   !?   ………
Scheme               Self          Java
(            )   (Prototype     OOP)   (   )




                  JavaScript
みゆっき☆Think#7 「本気で学ぶJavascript」
“
    Global Object
                               …”


                    http://brendaneich.com/tag/history/
JavaScript
MS
JScript

•Netscape
•JavaScript
JScript

•Netscape
•JavaScript

                        …
…
IE 3.0 Beta


JavaScript     !
IE 3.0


JavaScript   !
IE 3.0
IE 3.0
Netscape


•     MS       …

•
W3C?
…   …
…   …
※W3C
…
…
ECMA
ECMA

•

•Microsoft
ECMA Script(ES)
※
JavaScript
    ECMA
ES 1
•
•
•
•   IEEE754 (      …)

•
ES 2


•
•
ES 3
              ※


•
• String
• try/catch
•
ES 4
•
•
•
•   …

•
ES 4
•
•
•
•   …

•
ES 4
みゆっき☆Think#7 「本気で学ぶJavascript」
(   )
ES 5
    ※


• ES 3.1
•
• strict
• JSON
•
 http://kangax.github.com/es5-compat-table/
ES Harmony

• Class       ES 4                           ?

•2               ?

•         wiki


http://wiki.ecmascript.org/doku.php?id=harmony:harmony
5   JavaScript
Object
    JavaScript
Object
…
Object
                     Object
                      Object
            string   Function
  null
           number      Array
undefined
           boolean     String
                     Number
               ※     Boolean
                      RegExp
                        Date
                       etc...
…
Object
  JavScript
Object
  JavScript
     ※
Object
JavaScript
Object   ?
Object
Object
Object


         …
•       Object
    …
    …

•
    …            ?
•        Object      {}   new Object()
    {}            new Object()


           Java


• Object
                                 Object
みゆっき☆Think#7 「本気で学ぶJavascript」
Object
   Object
(__proto__)

         miyukki              Object.prototype

  name         miyukki     toString


  age              18          …


__proto__                  __proto__
(__proto__)

         miyukki                     Object.prototype

  name         miyukki            toString


  age              18                 …


__proto__                         __proto__



※ __proto__
                          IE
                         ES 5
みゆっき☆Think#7 「本気で学ぶJavascript」
Object
Object
•      Object
     null, undefined   Object
             Object
Object
•                Object
               null, undefined        Object
                       Object

• Object                        (=            )
       (   )    (         )
Object
•                   Object
                  null, undefined           Object
                          Object

• Object                             (=             )
       (      )     (        )



•          Object                  (__proto__)
                                      OK
Function   2
function sayHello(){
  console.log('Hello!');
}
> typeof sayHello
> typeof sayHello
    'function'
みゆっき☆Think#7 「本気で学ぶJavascript」
> typeof Object
> typeof Object
   'function'
みゆっき☆Think#7 「本気で学ぶJavascript」
'function' ?
Object
 Object
Function
  Array
  String
Number
Boolean
 RegExp
   Date
  etc...
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'
             ...
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
                 ...
Function
…
Function   2
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
new
      Function
みゆっき☆Think#7 「本気で学ぶJavascript」
1.       Object

2.           [[Class]]



3. prototype                      Object
     __proto__

4.           Object       this



5.                       Object
みゆっき☆Think#7 「本気で学ぶJavascript」
var miyukki = new Person('miyukki');
               miyukki.toString(); // Object.prototype




            Person                             Function                           Object

__proto__     Function.prototype   __proto__       Object.prototype   __proto__


 sayHello       <function>                 …        <function>                …       <function>




                         __proto__
みゆっき☆Think#7 「本気で学ぶJavascript」
Function
Function
• Function     new


 -                             prototype

     (__proto__)
 -           Prototype Chain
Function
• Function         new


 -                                   prototype

     (__proto__)
 -            Prototype Chain

•                        2
 - Function              prototype



 - Object            __proto__
               (     )
みゆっき☆Think#7 「本気で学ぶJavascript」
• ES5   new          Object.create



• ES3         null   prototype
         Object
Closure   ?
Closure   ?
     ※
…
JavaScript
2


•
•
2


         •
         •

※ with
<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>
<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>
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
※
<script type="text/javascript">
var hoge = ‘hoge’;

function add(n1, n2){
  var sum = n1 + n2;
  return sum;
}

add(10, 20);
</script>
n1              10


<script type="text/javascript">      n2              20

var hoge = ‘hoge’;                  sum         undefined


                                  arguments   {0: 10, 1: 20,...}
function add(n1, n2){
  var sum = n1 + n2;
  return sum;
}

add(10, 20);
</script>
n1               10



<script type="text/javascript">       n2               20


var hoge = ‘hoge’;                   sum            undefined


                                   arguments   {0: 10, 1: 20,...}

function add(n1, n2){
  var sum = n1 + n2;
  return sum;
}

add(10, 20);                hoge           'hoge'

</script>
                            add        <function>
n1              10
                                 hoge     'hoge'
   n2              20
                                 add    <function>
  sum         undefined


arguments   {0: 10, 1: 20,...}
みゆっき☆Think#7 「本気で学ぶJavascript」
Closure
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
Closure +
Closure +
•           2
-
-               =
Closure +
•           2
-
-                            =



•
-
                Activation


-
Closure +
Closure +
• Closure
 -
 - jQuery
 -
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
1. Object
            Object
1. Object
                Object



2. Function
     Function
     2
1. Object
                    Object



2. Function
     Function
     2

3. Closure
                2
?

More Related Content

みゆっき☆Think#7 「本気で学ぶJavascript」

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. &gt; var obj = {}\n&gt; obj\n{}\n&gt; typeof obj\n&apos;object&apos;\n&gt; Object.prototype.call.toString(obj);\nTypeError: Cannot call method &apos;toString&apos; of undefined\n at [object Context]:1:23\n at Interface.&lt;anonymous&gt; (repl.js:171:22)\n at Interface.emit (events.js:64:17)\n at Interface._onLine (readline.js:153:10)\n at Interface._line (readline.js:408:8)\n at Interface._ttyWrite (readline.js:585:14)\n at ReadStream.&lt;anonymous&gt; (readline.js:73:12)\n at ReadStream.emit (events.js:81:20)\n at ReadStream._emitKey (tty_posix.js:307:10)\n at ReadStream.onData (tty_posix.js:70:12)\n&gt; Object.prototype.toString.call(obj);\n&apos;[object Object]&apos;\n&gt; obj.toString()\n&apos;[object Object]&apos;\n&gt; obj\n{}\n&gt; obj.name = &apos;miyukki&apos;\n&apos;miyukki&apos;\n&gt; obj.age = 18\n18\n&gt; obj.age = &apos;18&apos;\n&apos;18&apos;\n&gt; obj\n{ name: &apos;miyukki&apos;, age: &apos;18&apos; }\n&gt; obj.age = 18\n18\n&gt; obj[&apos;age&apos;]\n18\n&gt; var key = &apos;age&apos;\n&gt; obj[key]\n18\n&gt; obj.sayHello = function(){ console.log(&apos;hi!&apos;) }\n[Function]\n&gt; obj\n{ name: &apos;miyukki&apos;, age: 18, sayHello: [Function] }\n&gt; obj.sayHello()\nhi!\n&gt; obj[&apos;sayHello&apos;]()\nhi!\n&gt; obj.child = { name: &apos;Miyukki Jr&apos;, age: 0 };\n{ name: &apos;Miyukki Jr&apos;, age: 0 }\n&gt; obj.child\n{ name: &apos;Miyukki Jr&apos;, age: 0 }\n&gt; obj\n{ name: &apos;miyukki&apos;,\n age: 18,\n sayHello: [Function],\n child: { name: &apos;Miyukki Jr&apos;, age: 0 } }\n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; new Date();\nThu, 01 Sep 2011 09:48:31 GMT\n&gt; var today = new Date();\n&gt; today.name = &apos;kyou no namae&apos;\n&apos;kyou no namae&apos;\n&gt; today\n{ Thu, 01 Sep 2011 09:48:35 GMT name: &apos;kyou no namae&apos; }\n&gt; today.name\n&apos;kyou no namae&apos;\n&gt; today.originalFunction = function(){ console.log(&apos;original function&apos;) } \n[Function]\n&gt; today.originalFunction()\noriginal function\n
  85. &gt; var obj = {}\n&gt; obj\n{}\n&gt; typeof obj\n&apos;object&apos;\n&gt; Object.prototype.call.toString(obj);\nTypeError: Cannot call method &apos;toString&apos; of undefined\n at [object Context]:1:23\n at Interface.&lt;anonymous&gt; (repl.js:171:22)\n at Interface.emit (events.js:64:17)\n at Interface._onLine (readline.js:153:10)\n at Interface._line (readline.js:408:8)\n at Interface._ttyWrite (readline.js:585:14)\n at ReadStream.&lt;anonymous&gt; (readline.js:73:12)\n at ReadStream.emit (events.js:81:20)\n at ReadStream._emitKey (tty_posix.js:307:10)\n at ReadStream.onData (tty_posix.js:70:12)\n&gt; Object.prototype.toString.call(obj);\n&apos;[object Object]&apos;\n&gt; obj.toString()\n&apos;[object Object]&apos;\n&gt; obj\n{}\n&gt; obj.name = &apos;miyukki&apos;\n&apos;miyukki&apos;\n&gt; obj.age = 18\n18\n&gt; obj.age = &apos;18&apos;\n&apos;18&apos;\n&gt; obj\n{ name: &apos;miyukki&apos;, age: &apos;18&apos; }\n&gt; obj.age = 18\n18\n&gt; obj[&apos;age&apos;]\n18\n&gt; var key = &apos;age&apos;\n&gt; obj[key]\n18\n&gt; obj.sayHello = function(){ console.log(&apos;hi!&apos;) }\n[Function]\n&gt; obj\n{ name: &apos;miyukki&apos;, age: 18, sayHello: [Function] }\n&gt; obj.sayHello()\nhi!\n&gt; obj[&apos;sayHello&apos;]()\nhi!\n&gt; obj.child = { name: &apos;Miyukki Jr&apos;, age: 0 };\n{ name: &apos;Miyukki Jr&apos;, age: 0 }\n&gt; obj.child\n{ name: &apos;Miyukki Jr&apos;, age: 0 }\n&gt; obj\n{ name: &apos;miyukki&apos;,\n age: 18,\n sayHello: [Function],\n child: { name: &apos;Miyukki Jr&apos;, age: 0 } }\n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; \n&gt; new Date();\nThu, 01 Sep 2011 09:48:31 GMT\n&gt; var today = new Date();\n&gt; today.name = &apos;kyou no namae&apos;\n&apos;kyou no namae&apos;\n&gt; today\n{ Thu, 01 Sep 2011 09:48:35 GMT name: &apos;kyou no namae&apos; }\n&gt; today.name\n&apos;kyou no namae&apos;\n&gt; today.originalFunction = function(){ console.log(&apos;original function&apos;) } \n[Function]\n&gt; today.originalFunction()\noriginal function\n
  86. \n
  87. \n
  88. node &amp;#x306F;&amp;#x99C4;&amp;#x76EE;\n\n\n
  89. var obj &amp;#x306E; __proto__ &amp;#x304C; Object.prototype &amp;#x3067;&amp;#x3042;&amp;#x308B;&amp;#x3053;&amp;#x3068;\n\n\n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. &amp;#x306A;&amp;#x3093;&amp;#x306E;&amp;#x5909;&amp;#x54F2;&amp;#x3082;&amp;#x306A;&amp;#x3044;&amp;#x95A2;&amp;#x6570;&amp;#x3067;&amp;#x3059;&amp;#x306D;\n
  96. &amp;#x306A;&amp;#x3093;&amp;#x306E;&amp;#x5909;&amp;#x54F2;&amp;#x3082;&amp;#x306A;&amp;#x3044;&amp;#x95A2;&amp;#x6570;&amp;#x3067;&amp;#x3059;&amp;#x306D;\n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. \n
  107. \n
  108. \n
  109. \n
  110. \n
  111. \n
  112. \n
  113. \n
  114. \n
  115. \n
  116. \n
  117. \n
  118. \n
  119. \n
  120. \n
  121. \n
  122. \n
  123. \n
  124. \n
  125. \n
  126. \n
  127. \n
  128. \n
  129. \n
  130. \n
  131. \n
  132. \n
  133. \n
  134. \n
  135. \n
  136. \n
  137. \n
  138. \n