{"id":3867,"date":"2016-11-16T05:56:30","date_gmt":"2016-11-15T20:56:30","guid":{"rendered":"http:\/\/stuffy.dip.jp\/wordpress\/?p=2415"},"modified":"2016-11-16T05:56:30","modified_gmt":"2016-11-15T20:56:30","slug":"closure-in-javascript-2-2","status":"publish","type":"post","link":"https:\/\/www.stuffy.site\/wordpress\/?p=3867","title":{"rendered":"javascript\u306e\u30af\u30ed\u30fc\u30b8\u30e3"},"content":{"rendered":"<p>\u30e1\u30e2\u5316\u3055\u308c\u305f fibonacci.js<\/p>\n<pre class=\"brush: actionscript3; gutter: false; first-line: 1\"><span style=\"font-size: 10pt;\">var fibonacci = function () {\n\u00a0\u00a0\u00a0 var memo = [0, 1];\n\u00a0\u00a0\u00a0 var fib = function (n) {\n\u00a0\u00a0\u00a0\u00a0\u00a0 var result = memo[n];\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (typeof result !== 'number') {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result = fib(n -1) + fib(n -2);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 memo[n] = result;\n\u00a0\u00a0\u00a0\u00a0\u00a0 };\n\u00a0\u00a0\u00a0\u00a0\u00a0 return result;\n\u00a0\u00a0\u00a0 };\n\u00a0\u00a0\u00a0 return fib;\n}();\nfor (var i = 0; i &lt;=100; i +=1) {\n\u00a0\u00a0\u00a0 document.writeln('\/\/ ' + i + ': ' + fibonacci(i) +'&lt;br&gt;');\n}\n<\/span><\/pre>\n<p>\u5165\u308c\u5b50\u72b6\u306b\u306a\u3063\u305f\u95a2\u6570\u306e\u623b\u308a\u5024\u3092\u3001\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u8a18\u9332\uff08\u30e1\u30e2\u5316\uff09\u3059\u308b\u3068\u3001\u518d\u5e30\u7684\u306a\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u3092\u5927\u5e45\u306b\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30e1\u30e2\u5316\u306b\u3064\u3044\u3066\u306f<a href=\"http:\/\/blog.h13i32maru.jp\/entry\/20100924\/1285346166\">\u30af\u30ed\u30fc\u30b8\u30e3\u3092\u4f7f\u3063\u3066\u30e1\u30e2\u5316 &#8211; maru source\u00a0<\/a>\u00a0\u3067\u3001\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092\u4f8b\u306b\u8a73\u3057\u304f\u89e3\u8aac\u3055\u308c\u3066\u3044\u307e\u3059\u306e\u3067\u5272\u611b\u3057\u3066\u3001\u4eca\u56de\u306f\u95a2\u6570\u306e\u30af\u30ed\u30fc\u30b8\u30e3\u306b\u3064\u3044\u3066\u3044\u307e\u3044\u3061\u826f\u304f\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<blockquote><p>8.6 \u30af\u30ed\u30fc\u30b8\u30e3<\/p><\/blockquote>\n<p><em><span style=\"font-family: Mincho; font-size: 10pt;\">\u6700\u8fd1\u306e\u591a\u304f\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u8a00\u8a9e\u3068\u540c\u3058\u3088\u3046\u306b\u3001JavaScript \u3067\u306f\u69cb\u6587\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3044\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u306f\u3001\u95a2\u6570\u304c<strong>\u5b9a\u7fa9\u3055\u308c\u305f\u3068\u304d\u306b<\/strong>\u6709\u52b9\u3067\u3042\u3063\u305f\u5909\u6570\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3044\u307e\u3059\u3002\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u3068\u304d\u306b\u6709\u52b9\u306a\u5909\u6570\u30b9\u30b3\u30fc\u30d7\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002(\u4e2d\u7565) \u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3001\u95a2\u6570\u306e\u5909\u6570\u306e\u540d\u524d\u89e3\u6c7a\u306b\u4f7f\u308f\u308c\u308b\u30b9\u30b3\u30fc\u30d7\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u3082\u306e\u3092\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b5\u30a4\u30a8\u30f3\u30b9\u306e\u5206\u91ce\u3067\u306f\u30af\u30ed\u30fc\u30b8\u30e3\u3068\u547c\u3093\u3067\u3044\u307e\u3059\u3002<br \/>\n\u6b63\u78ba\u306b\u8a00\u3048\u3070\u3001JavaScript \u306e\u95a2\u6570\u306f\u3059\u3079\u3066\u30af\u30ed\u30fc\u30b8\u30e3\u3067\u3059\u3002<br \/>\n<\/span><\/em><br \/>\n\u3053\u3053\u307e\u3067\u306f\u4f55\u3092\u8a00\u3063\u3066\u3044\u308b\u306e\u304b\u826f\u304f\u89e3\u308a\u307e\u305b\u3093\u306d\u3002\u5b9f\u969b\u306b\u30b3\u30fc\u30c9\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n<em><span style=\"font-family: Mincho; font-size: 10pt;\">\u30af\u30ed\u30fc\u30b8\u30e3\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u307e\u305a\u5165\u308c\u5b50\u578b\u95a2\u6570\u306e\u69cb\u6587\u30b9\u30b3\u30fc\u30d7\u306b\u3064\u3044\u3066\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/span><\/em><\/p>\n<pre class=\"brush: actionscript3; gutter: false; first-line: 1\"><span style=\"font-size: 10pt;\">var scope = \"global scope\";\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3002\nfunction checkscope() {\n\u00a0\u00a0\u00a0 var scope = \"local scope\";\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3002\n\u00a0\u00a0\u00a0 function f() { return scope; }\u00a0 \/\/ \u3053\u3053\u3067\u306e\u30b9\u30b3\u30fc\u30d7\u4e2d\u306e\u5024\u304c\u623b\u3055\u308c\u308b\u3002\n\u00a0\u00a0\u00a0 return f();\n}\ncheckscope() \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ =&gt; \"local scope\"<\/span><\/pre>\n<p><em><span style=\"font-family: Mincho; font-size: 10pt;\">checkscope()\u95a2\u6570\u306f\u3001\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092\u5ba3\u8a00\u3057\u3001\u305d\u306e\u5f8c\u3001\u3053\u306e\u5909\u6570\u306e\u5024\u3092\u8fd4\u3059\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002checkscope() \u3092\u547c\u3073\u51fa\u3057\u305f\u3068\u304d\u306b\u3001&#8221;local scope&#8221; \u304c\u623b\u3055\u308c\u308b\u3053\u3068\u306b\u7591\u554f\u3092\u62b1\u304b\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u5c11\u3057\u5909\u66f4\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u6b21\u306e\u30b3\u30fc\u30c9\u3067\u306f\u4f55\u304c\u8fd4\u3055\u308c\u308b\u304b\u308f\u304b\u308a\u307e\u3059\u304b\u3002<\/span><\/em><\/p>\n<pre class=\"brush: actionscript3; gutter: false; first-line: 1\"><span style=\"font-size: 10pt;\">var scope = \"global scope\";\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3002\nfunction checkscope() {\n\u00a0\u00a0\u00a0 var scope = \"local scope\"; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ \u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3002\n\u00a0\u00a0\u00a0 function f() { return scope; }  \/\/ \u3053\u3053\u3067\u306e\u30b9\u30b3\u30fc\u30d7\u4e2d\u306e\u5024\u304c\u623b\u3055\u308c\u308b\u3002\n\u00a0\u00a0\u00a0 return f;\n}\ncheckscope()() \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ \u4f55\u304c\u8fd4\u3055\u308c\u308b\u304b\uff1f<\/span><\/pre>\n<p><em><span style=\"font-family: Mincho; font-size: 10pt;\">\u4eca\u56de\u306e\u30b3\u30fc\u30c9\u3067\u306f\u62ec\u5f27\u304ccheckscope()\u304b\u3089\u5916\u306b\u79fb\u52d5\u3057\u305f\u3060\u3051\u3067\u3059\u3002\u5165\u308c\u5b50\u578b\u306e\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3066\u623b\u308a\u3092\u8fd4\u3059\u306e\u3067\u306f\u306a\u304f\u3001<strong>\u5165\u308c\u5b50\u578b\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u81ea\u8eab\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059<\/strong>\u3002\u62ec\u5f27\u3092\u30b3\u30fc\u30c9\u306e\u6700\u5f8c\u306e\u884c\u306b\u52d5\u304b\u3059\u3053\u3068\u3067\u3001\u5165\u308c\u5b50\u578b\u306e\u95a2\u6570\u3092<strong>\u5b9a\u7fa9\u3057\u305f\u5834\u6240\u306e\u5916\u5074\u3067\u547c\u3073\u51fa\u3057\u305f\u5834\u5408<\/strong>\u3001\u3069\u3046\u306a\u308b\u3067\u3057\u3087\u3046\u3002\u69cb\u6587\u30b9\u30b3\u30fc\u30d7\u306e\u57fa\u672c\u3068\u306a\u308b\u898f\u5247\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002JavaScript \u306e\u95a2\u6570\u306f\u3001<strong>\u5b9a\u7fa9\u3055\u308c\u305f\u3068\u304d\u306b<\/strong>\u6709\u52b9\u306a\u30b9\u30b3\u30fc\u30d7\u30c1\u30a7\u30fc\u30f3\u3092\u4f7f\u3063\u3066\u3001\u5b9f\u884c\u3055\u308c\u308b\u3001\u3068\u3044\u3046\u898f\u5247\u3067\u3059\u3002\u5165\u308c\u5b50\u578b\u306e\u95a2\u6570 f() \u306f\u3001\u5909\u6570 scope \u306e\u5024\u304c &#8220;local scope&#8221; \u306b\u30d0\u30a4\u30f3\u30c9\uff08\u675f\u7e1b\uff09\u3055\u308c\u3066\u3044\u308b\u30b9\u30b3\u30fc\u30d7\u30c1\u30a7\u30fc\u30f3\u306e\u4e0b\u3067<strong>\u5b9a\u7fa9\u3055\u308c\u307e\u3057\u305f<\/strong>\u3002\u3053\u306e\u5909\u6570\u30d0\u30a4\u30f3\u30c9\u306f\u3001 f \u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u3082\u6709\u52b9\u3067\u3059\u3002\u3069\u3053\u3067\u547c\u3073\u51fa\u3055\u308c\u305f\u3068\u3057\u3066\u3082\u3001\u3053\u308c\u306f\u5909\u308f\u308a\u307e\u305b\u3093\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5148\u306e\u30b3\u30fc\u30c9\u306e\u6700\u5f8c\u306e\u884c\u304c\u8fd4\u3059\u306e\u306f\u00a0 &#8220;local scope&#8221; \u3067\u3059\u3002&#8221;global scope&#8221; \u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/span><\/em><\/p>\n<p>\u5148\u306e fibonacci.js \u306e\u4f8b\u3067\u306f\u5165\u308c\u5b50\u306b\u306a\u3063\u305f\u30ed\u30fc\u30ab\u30eb \u95a2\u6570\u3001 fib = function (n) \u304cfibonacci(i)\u3067\u547c\u3073\u51fa\u3055\u308c\u3066\u3001\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u304c\u8fd4\u3055\u308c\u305f\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u3002();    \t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30e1\u30e2\u5316\u3055\u308c\u305f fibonacci.js var fibonacci = function () { \u00a0\u00a0\u00a0 var memo = [0, 1]; \u00a0\u00a0\u00a0 var fib = function (n) { \u00a0\u00a0\u00a0\u00a0\u00a0 va &hellip; <a href=\"https:\/\/www.stuffy.site\/wordpress\/?p=3867\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">javascript\u306e\u30af\u30ed\u30fc\u30b8\u30e3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87,5],"tags":[],"class_list":["post-3867","post","type-post","status-publish","format-standard","hentry","category-javascript","category-software"],"_links":{"self":[{"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3867"}],"version-history":[{"count":0,"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3867\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuffy.site\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}