{"id":1338,"date":"2013-10-22T00:00:39","date_gmt":"2013-10-21T15:00:39","guid":{"rendered":"https:\/\/www.united-bears.co.jp\/blog\/?p=1338"},"modified":"2013-10-22T00:20:12","modified_gmt":"2013-10-21T15:20:12","slug":"javascript-%e3%81%ae%e3%82%af%e3%83%ad%e3%83%bc%e3%82%b8%e3%83%a3%e3%81%a8%e3%83%ab%e3%83%bc%e3%83%97%e3%80%81%e5%a4%89%e6%95%b0%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"https:\/\/www.united-bears.co.jp\/blog\/archives\/1338","title":{"rendered":"JavaScript \u306e\u30af\u30ed\u30fc\u30b8\u30e3\u3068\u30eb\u30fc\u30d7\u3001\u5909\u6570\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>JavaScript \u306e\u30af\u30ed\u30fc\u30b8\u30e3\u306f\u4fbf\u5229\u306a\u6a5f\u80fd\u3060\u304c\u3001\u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u5b9a\u7fa9\u3059\u308b\u3068\u610f\u56f3\u3057\u306a\u3044\u5024\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b\u3002\u305f\u3068\u3048\u3070\u3001for \u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u30af\u30ed\u30fc\u30b8\u30e3\u3092\u5b9a\u7fa9\u3057\u3066\u3001\u30eb\u30fc\u30d7\u5909\u6570\u306e\u5024\u3092\u5229\u7528\u3057\u305f\u3089\u3001\u3059\u3079\u3066\u30eb\u30fc\u30d7\u306e\u6700\u5f8c\u306e\u5024\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u3059\u3079\u3066\u306e\u95a2\u6570\u306e\u4e2d\u3067\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53c2\u7167\u3057\u3066\u3057\u307e\u3046\u306a\u3069\u3001\u5931\u6557\u4f8b\u306f\u679a\u6319\u306b\u3044\u3068\u307e\u304c\u306a\u3044\u3002\u7121\u540d\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u3068\u8a18\u8ff0\u304c\u8907\u96d1\u306b\u306a\u3063\u3066\u3001\u3044\u3064\u3082\u4ee5\u524d\u306e\u30b3\u30fc\u30c9\u3092\u63a2\u3057\u56de\u3063\u3066\u66f8\u304f\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u30e1\u30e2\u3092\u6b8b\u3059\u3053\u3068\u306b\u3057\u305f\u3002<\/p>\n<p>\u30af\u30ed\u30fc\u30b8\u30e3\u306e\u5b9a\u7fa9\u3092\u8a00\u8449\u3067\u8aac\u660e\u3059\u308b\u306e\u306f\u96e3\u3057\u304f\u3001<a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E3%82%AF%E3%83%AD%E3%83%BC%E3%82%B8%E3%83%A3\" target=\"_blank\">Wikipedia<\/a>\u306b\u3082\u3001\u300c\u30af\u30ed\u30fc\u30b8\u30e3\uff08\u30af\u30ed\u30fc\u30b8\u30e3\u30fc\u3001\u82f1: closure\uff09\u3001\u95a2\u6570\u9589\u5305\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4e00\u7a2e\u3002\u3044\u304f\u3064\u304b\u306e\u8a00\u8a9e\u3067\u306f\u30e9\u30e0\u30c0\u5f0f\u3084\u7121\u540d\u95a2\u6570\u3067\u5b9f\u73fe\u3057\u3066\u3044\u308b\u3002\u5f15\u6570\u4ee5\u5916\u306e\u5909\u6570\u3092\u5b9f\u884c\u6642\u306e\u74b0\u5883\u3067\u306f\u306a\u304f\u3001\u81ea\u8eab\u304c\u5b9a\u7fa9\u3055\u308c\u305f\u74b0\u5883\uff08\u9759\u7684\u30b9\u30b3\u30fc\u30d7\uff09\u306b\u304a\u3044\u3066\u89e3\u6c7a\u3059\u308b\u3053\u3068\u3092\u7279\u5fb4\u3068\u3059\u308b\u3002\u300d\u3068\u66f8\u3044\u3066\u3042\u308b\u304c\u8aad\u3093\u3067\u3082\u76f4\u611f\u7684\u306b\u7406\u89e3\u3067\u304d\u306a\u3044\u3002\u3056\u3063\u304f\u308a\u3068\u3001\u95a2\u6570\u306e\u4e2d\u306b\u30ed\u30fc\u30ab\u30eb\u306a\u30b9\u30b3\u30fc\u30d7\u306e\u5909\u6570\u3092\u5b9a\u7fa9\u3067\u304d\u308b\u3068\u8003\u3048\u3066\u304a\u3051\u3070\u3044\u3044\u3060\u308d\u3046\u304b\u3002MOZILLA DEVELOPER NETWORK \u306e<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Guide\/Closures\" target=\"_blank\">\u30af\u30ed\u30fc\u30b8\u30e3\u306e\u4f7f\u7528<\/a>\u306e\u65b9\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<p>\u6700\u8fd1\u306f jQuery \u3092\u3088\u304f\u4f7f\u3046\u306e\u3067\u3001jQuery \u3092\u4f7f\u3063\u305f\u30b5\u30f3\u30d7\u30eb\u3092\u6b8b\u3057\u3066\u304a\u304f\u3053\u3068\u306b\u3057\u305f\u3002\u57fa\u672c\u3068\u306a\u308b HTML \u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5358\u7d14\u306a\u3082\u306e\u3092\u5229\u7528\u3059\u308b\u3002\u5404 a \u30bf\u30b0\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u304d\u306e\u52d5\u4f5c\u3092\u3001jQuery \u3067\u5f8c\u304b\u3089\u5b9a\u7fa9\u3057\u3066\u3044\u304f\u3053\u3068\u306b\u3059\u308b\u3002<\/p>\n<pre>\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script src=\"jquery.js\" type=\"text\/javascript\" charset=\"utf-8\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;a href=\"javascript:void(0);\"&gt;a&lt;\/a&gt;\r\n&lt;a href=\"javascript:void(0);\"&gt;b&lt;\/a&gt;\r\n&lt;a href=\"javascript:void(0);\"&gt;c&lt;\/a&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h2>\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u53c2\u7167\u3059\u308b<\/h2>\n<p>\u6700\u3082\u5358\u7d14\u306a\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u53c2\u7167\u3059\u308b\u65b9\u6cd5\u3060\u3002\u3053\u306e\u5834\u5408\u3001\u30ea\u30f3\u30af\u306e a b c \u3069\u308c\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u3082\u3001i \u306e\u5024\u306f\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3055\u308c\u308b\u3002\u5909\u6570\u304c\u6574\u6570\u3060\u3068\u4f7f\u3044\u9053\u306f\u3042\u307e\u308a\u306a\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u304c\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u5b9a\u7fa9\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5024\u3092\u5909\u66f4\u3059\u308b\u3068\u304d\u306a\u3069\u306f\u3001\u3053\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u3046\u3053\u3068\u3082\u3042\u308b\u3002\u305f\u3060\u3057\u3001XMLHttpRequest \u3092\u975e\u540c\u671f\u3067\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u975e\u540c\u671f\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u66f4\u65b0\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308b\u305f\u3081\u5024\u304c\u4e0d\u5b9a\u306b\u306a\u308b\u3002<\/p>\n<pre>\r\nvar i = 0;\r\n$('a').each(function() {\r\n    $(this).click(function() {\r\n        console.log(i++);\r\n    });\r\n});\r\n<\/pre>\n<h2>\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092\u53c2\u7167\u3059\u308b<\/h2>\n<p>\u95a2\u6570\u306e\u5185\u90e8\u3067\u5b9a\u7fa9\u3057\u305f\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092\u53c2\u7167\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u3067\u3001a \u3092\u9023\u7d9a\u3057\u3066\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u30011 2 3\u3068\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u51fa\u529b\u3059\u308b\u3002\u305f\u3060\u3057\u3001\u6b21\u306b b \u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30824\u306b\u306f\u306a\u3089\u305a\u30011\u306b\u306a\u308b\u3002\u3064\u307e\u308a\u3001\u5404 a \u30bf\u30b0\u3054\u3068\u306b\u5909\u6570 i \u304c\u30ed\u30fc\u30ab\u30eb\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3001\u5024\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u5f62\u306b\u306a\u308b\u3002<\/p>\n<pre>\r\n$('a').each(function() {\r\n    var i = 0;\r\n    $(this).click(function() {\r\n        console.log(i++);\r\n    });\r\n});\r\n<\/pre>\n<h2>\u3088\u304f\u3042\u308b\u5931\u6557<\/h2>\n<p>\u4e0b\u8a18\u306e\u30b3\u30fc\u30c9\u3060\u3068\u3001\u5404 a \u30bf\u30b0\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u7d50\u679c\u306f\u5fc5\u305a3\u306b\u306a\u308b\u3002\u3088\u304f\u3042\u308b\u5931\u6557\u306e\u4f8b\u3060\u3002\u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u308b\u308f\u3051\u3060\u304c\u3001\u30eb\u30fc\u30d7\u5909\u6570\u306f\u95a2\u6570\u306e\u5916\u90e8\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3002\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3059\u308b\u95a2\u6570\u306f\u3001\u5b9f\u884c\u6642\u306b i \u3092\u53c2\u7167\u3059\u308b\u304c\u3001\u305d\u308c\u306f\u30eb\u30fc\u30d7\u304c\u7d42\u308f\u3063\u305f\u3068\u304d\u306e\u5024\u3060\u3002\u5f93\u3063\u3066\u3001\u4eca\u56de\u306e\u4f8b\u306e\u5834\u5408\u306f3\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n<pre>\r\nvar array = new Array();\r\n$('a').each(function() {\r\n    array.push($(this));\r\n});\r\n\r\nfor(var i = 0; i < array.length; i++) {\r\n    array[i].click(function() {\r\n        console.log(i);\r\n    });\r\n}\r\n<\/pre>\n<h2>\u30eb\u30fc\u30d7\u5185\u3067\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u5408\u306f return function \u304c\u5fc5\u8981\u304b\u3082\u3057\u308c\u306a\u3044<\/h2>\n<p>\u30eb\u30fc\u30d7\u5185\u3067\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u3066\u3001\u30eb\u30fc\u30d7\u5909\u6570\u306e\u5024\u3092\u8868\u793a\u3059\u308b\u306b\u306f\u3001\u95a2\u6570\u3092\u623b\u3059\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\u4e0b\u8a18\u306e\u4f8b\u3067\u306f\u3001\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u308b\u95a2\u6570\u306f\u3001\u95a2\u6570\u3092\u623b\u3057\u3066\u304a\u308a\u3001\u304b\u3064\u3001\u95a2\u6570\u81ea\u4f53\u3092\u547c\u3073\u51fa\u3059\u3088\u3046\u306b\u6307\u5b9a\u3057\u3066\u5f15\u6570\u306b\u30eb\u30fc\u30d7\u5909\u6570\u3092\u6e21\u3057\u3066\u3044\u308b\u3002console.log \u3067\u6e21\u3057\u3066\u3044\u308b\u5f15\u6570\u306f\u3001\u6700\u521d\u306e\u95a2\u6570\u306e\u5f15\u6570\u3068\u540c\u3058\u3082\u306e\u3092\u53c2\u7167\u3059\u308b\u306e\u3067\u3001\u30eb\u30fc\u30d7\u3057\u3066\u3044\u308b\u9593\u306e i \u306e\u5024\u3092\u8868\u793a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<pre>\r\nvar array = new Array();\r\n$('a').each(function() {\r\n    array.push($(this));\r\n});\r\n\r\nfor(var i = 0; i < array.length; i++) {\r\n    array[i].click((function(n) {\r\n        return function() {\r\n            console.log(n);\r\n        }\r\n    }).call(this, i));\r\n}\r\n<\/pre>\n<p>\u3053\u306e\u8a18\u8ff0\u65b9\u6cd5\u3092\u3044\u3064\u3082\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u30e1\u30e2\u3067\u6b8b\u3057\u3066\u304a\u304d\u305f\u304b\u3063\u305f\u306e\u3067\u3059\u3002\u7121\u540d\u95a2\u6570\u306b\u3057\u306a\u3044\u3067\u5909\u6570\u306b\u95a2\u6570\u3092\u5272\u308a\u5f53\u3066\u305f\u308a\u3059\u308c\u3070\u3088\u3044\u306e\u304b\u3082\u3057\u308c\u306a\u3044\u3051\u3069\u3001\u77ed\u3044\u51e6\u7406\u3092\u66f8\u304f\u306e\u306b\u7121\u540d\u95a2\u6570\u3092\u4f7f\u308f\u306a\u3044\u3068\u30b9\u30de\u30fc\u30c8\u306b\u304b\u3051\u306a\u3044\u306e\u3067\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u306e\u30af\u30ed\u30fc\u30b8\u30e3\u306f\u4fbf\u5229\u306a\u6a5f\u80fd\u3060\u304c\u3001\u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u5b9a\u7fa9\u3059\u308b\u3068\u610f\u56f3\u3057\u306a\u3044\u5024\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b\u3002\u305f\u3068\u3048\u3070\u3001for \u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u30af\u30ed\u30fc\u30b8\u30e3\u3092\u5b9a\u7fa9\u3057\u3066\u3001\u30eb\u30fc\u30d7\u5909\u6570\u306e\u5024\u3092\u5229\u7528\u3057\u305f\u3089\u3001\u3059\u3079\u3066\u30eb\u30fc\u30d7\u306e\u6700\u5f8c\u306e\u5024\u306b\u306a\u3063\u3066\u3057\u307e\u3063 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1338","post","type-post","status-publish","format-standard","hentry","category-web-develop"],"_links":{"self":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/1338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=1338"}],"version-history":[{"count":11,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/1338\/revisions"}],"predecessor-version":[{"id":1373,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/1338\/revisions\/1373"}],"wp:attachment":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=1338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=1338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=1338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}