{"id":116747,"date":"2021-12-31T06:00:18","date_gmt":"2021-12-30T21:00:18","guid":{"rendered":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/?p=116747"},"modified":"2022-05-26T22:13:59","modified_gmt":"2022-05-26T13:13:59","slug":"javascript-bind","status":"publish","type":"post","link":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/2021\/12\/javascript-bind.html","title":{"rendered":"\u3010JavaScript\u306e\u5fdc\u7528\u3011this\u306e\u675f\u7e1b -bind"},"content":{"rendered":"<p>this\u306e\u5024\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308bbind\u30e1\u30bd\u30c3\u30c9\u3002<br \/>\nbind\u3068\u3044\u3046\u540d\u524d\u306e\u901a\u308a\u3001this\u3092\u675f\u7e1b\u3059\u308b\u305f\u3081\u306b\u5229\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001this\u3092\u81ea\u7531\u306b\u5236\u5fa1\u3059\u308b\u305f\u3081\u306b\u4fbf\u5229\u306abind\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u89e3\u8aac\u3068\u3001\u985e\u4f3c\u306ecall\/apply\u30e1\u30bd\u30c3\u30c9\u3068\u306e\u9055\u3044\u306b\u3064\u3044\u3066\u53d6\u308a\u4e0a\u3052\u307e\u3059\u3002<\/p>\n<h2 class=\"style2c\">bind\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>bind\u30e1\u30bd\u30c3\u30c9\u3068\u306f\u3001this\u306e\u5024\u3092\u675f\u7e1b\u3057\u3001\u65b0\u3057\u3044\u95a2\u6570\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<br \/>\nthis\u306e\u675f\u7e1b\u4ee5\u5916\u306b\u3082\u3001\u300c\u95a2\u6570\u306e\u7d10\u4ed8\u3051\u300d\u306a\u3069\u3069\u8868\u73fe\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>bind\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u308b\u3068\u3001\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u4e0e\u3048\u3089\u308c\u305fthis\u306e\u5024\u3092\u6301\u3063\u305f\u65b0\u3057\u3044\u95a2\u6570\u3092\u751f\u6210\u3059\u308b\u3068\u3044\u3046\u306e\u304c\u7279\u5fb4\u3067\u3059\u3002<\/p>\n<h2 class=\"style2c\">bind\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u3044\u65b9<\/h2>\n<p>\u3067\u306f\u3001\u5177\u4f53\u7684\u306bbind\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u3044\u65b9\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3 class=\"style3c\">bind\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u69cb\u6587<\/h3>\n<p>\u4ee5\u4e0b\u306f\u3001bind\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u69cb\u6587\u3067\u3059\u3002<br \/>\n\u7b2c\u4e00\u5f15\u6570\u306bthis\u306e\u5024\u3092\u6307\u5b9a\u3057\u3001\u7b2c\u4e8c\u5f15\u6570\u4ee5\u964d\u306b\u306f\u7d10\u4ed8\u3051\u305f\u3044\u95a2\u6570\u306e\u5f15\u6570\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>\u95a2\u6570.bind(this\u306e\u5024, ...\u95a2\u6570\u306e\u5f15\u6570);\r\n<\/code><\/pre>\n<\/div>\n<h3 class=\"style3c\">bind\u30e1\u30bd\u30c3\u30c9\u306e\u5f79\u5272<\/h3>\n<p>bind\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>let user = {\r\n  name: 'Tarou'\r\n};\r\n\r\nfunction greeting(message) {\r\n  return `${message}, ${this.name}!`;\r\n}\r\n\r\n\/\/ \u95a2\u6570greeting\u3092\u30e9\u30c3\u30d7\u3057\u305f\u65b0\u3057\u3044\u95a2\u6570\uff08getUser\uff09\u3092\u4f5c\u308b\r\nconst getUser = greeting.bind(user, 'Hello');\r\nconsole.log(getUser()); \/\/ \"Hello, Tarou!\"\r\n<\/code><\/pre>\n<\/div>\n<p>\u95a2\u6570greeting\u3067\u306f\u3001this\u304cuser\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u675f\u7e1b\u3055\u308c\u3001\u3055\u3089\u306bbind\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u3063\u3066\u95a2\u6570greeting\u304c\u30e9\u30c3\u30d7\u3055\u308c\u305f\u65b0\u3057\u3044\u95a2\u6570\u304c\u751f\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e0a\u8a18\u306e\u4f8b\u3092\u5358\u7d14\u306a\u95a2\u6570\u3067\u8868\u73fe\u3059\u308b\u3068\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>let user = {\r\n  name: 'Tarou'\r\n};\r\n\r\nfunction greeting(message) {\r\n  return `${message}, ${this.name}!`;\r\n}\r\n\r\n\/\/ bind\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5143\u306e\u95a2\u6570\u3092\u30e9\u30c3\u30d7\u3059\u308b\u65b0\u3057\u3044\u95a2\u6570\u3092\u4f5c\u308b\r\nconst getUser = () =&gt; {\r\n  return greeting.call(user, 'Hello');\r\n};\r\n\r\nconsole.log(getUser()); \/\/ \"Hello, Tarou!\"\r\n<\/code><\/pre>\n<\/div>\n<p>\u3044\u304b\u304c\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\nbind\u30e1\u30bd\u30c3\u30c9\u306f\u3001this\u3084\u305d\u306e\u4ed6\u306e\u5f15\u6570\u3092\u675f\u7e1b\u3057\u305f\u95a2\u6570\u3092\u4f5c\u308b\u5f79\u5272\u304c\u3042\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2 class=\"style2c\">call\/apply\u30e1\u30bd\u30c3\u30c9\u3068\u306e\u9055\u3044<\/h2>\n<p>bind\u30e1\u30bd\u30c3\u30c9\u306f\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u3068\u6df7\u540c\u3055\u308c\u304c\u3061\u3067\u3059\u304c\u3001\u7570\u306a\u308b\u52d5\u304d\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306e\u4f8b\u3092\u898b\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>\/\/ bind\u30e1\u30bd\u30c3\u30c9\u306e\u30d1\u30bf\u30fc\u30f3\r\nlet user = {\r\n  name: 'Tarou',\r\n  greeting: function() {\r\n    console.log(`Hello, ${this.name}!`);\r\n  }\r\n};\r\n\r\nconst sayHi = user.greeting.bind(user); \/\/ this\u3092\u675f\u7e1b\u3057\u305f\u95a2\u6570sayHi\u3092\u4f5c\u308b\r\nsayHi(); \/\/ \"Hello, Tarou!\"\r\nsetTimeout(sayHi, 2000); \/\/ \"Hello, Tarou!\" 2\u79d2\u5f8c\u306b\u95a2\u6570sayHi\u304c\u5b9f\u884c\u3055\u308c\u308b\r\n\r\n\r\n\/\/ call\u30e1\u30bd\u30c3\u30c9\u306e\u30d1\u30bf\u30fc\u30f3(apply\u30e1\u30bd\u30c3\u30c9\u3082\u540c\u69d8)\r\nlet user = {\r\n  name: 'Tarou',\r\n  greeting: function() {\r\n    console.log(`Hello, ${this.name}!`);\r\n  }\r\n};\r\n\r\nconst sayHi = user.greeting.call(user); \/\/ \"Hello, Tarou!\" \u3053\u3053\u3067this\u3092\u6307\u5b9a\u3057\u305f\u95a2\u6570greeting\u304c\u5b9f\u884c\u3055\u308c\u308b\r\nsayHi(); \/\/ \"Uncaught TypeError: sayHi is not a function\"\r\n<\/code><\/pre>\n<\/div>\n<p>bind\/call\/apply\u30e1\u30bd\u30c3\u30c9\u306f\u3001this\u306e\u5024\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u3044\u3046\u70b9\u306f\u540c\u3058\u3067\u3059\u3002<br \/>\n\u3057\u304b\u3057\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u304c\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u305f\u77ac\u6642\u306b\u5143\u306e\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u308b\u306e\u306b\u5bfe\u3057\u3066\u3001bind\u30e1\u30bd\u30c3\u30c9\u306f\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u305f\u3068\u540c\u6642\u306b\u65b0\u3057\u3044\u95a2\u6570\u3092\u4f5c\u308a\u3001\u65b0\u3057\u3044\u95a2\u6570\u306bthis\u3084\u5f15\u6570\u306e\u5024\u3092\u5f15\u7d99\u304e\u307e\u3059\u3002<\/p>\n<div>\n<div><span>call\/apply\u306f\u3001\u65b0\u3057\u3044\u95a2\u6570\u3092\u4f5c\u6210\u3059\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u305f\u3081\u3001\u95a2\u6570sayHi\u3092\u5b9f\u884c\u3057\u3066\u3082\u95a2\u6570\u3068\u3057\u3066\u8a8d\u8b58\u3055\u308c\u305a\u30a8\u30e9\u30fc\u3068\u306a\u308a\u307e\u3059\u3002<\/span><\/div>\n<\/div>\n<h2 class=\"style2c\">\u307e\u3068\u3081<\/h2>\n<p>\u4eca\u56de\u306f\u3001bind\u30e1\u30bd\u30c3\u30c9\u306e\u5f79\u5272\u3068\u985e\u4f3c\u306ecall\/apply\u30e1\u30bd\u30c3\u30c9\u3068\u306e\u9055\u3044\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>bind\u30e1\u30bd\u30c3\u30c9\u3068\u4f7f\u3046\u3068\u3001this\u306e\u5024\u3092\u56fa\u5b9a\u5316\u3057\u3001this\u3084\u5f15\u6570\u3092\u65b0\u3057\u3044\u95a2\u6570\u3078\u7d10\u4ed8\u3051\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\ncall\/apply\u30e1\u30bd\u30c3\u30c9\u3068\u4f3c\u305f\u3088\u3046\u306a\u5f79\u5272\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u307e\u305a\u306f\u3001\u300cthis\u3092\u675f\u7e1b\u3057\u3066\u65b0\u3057\u3044\u95a2\u6570\u3092\u4f5c\u308b\u306e\u304cbind\u30e1\u30bd\u30c3\u30c9\u300d\u3068\u899a\u3048\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>bind\u30e1\u30bd\u30c3\u30c9\u3092\u3046\u307e\u304f\u6d3b\u7528\u3057\u3066\u3001this\u3092\u81ea\u7531\u81ea\u5728\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u5b9f\u8df5\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2 class=\"style5\">\u5408\u308f\u305b\u3066\u8aad\u307f\u305f\u3044this\u30b7\u30ea\u30fc\u30ba<\/h2>\n<p>\u7b2c1\u56de\uff1a<a href=\"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/2021\/12\/javascript-this.html\"><strong>this\u306e\u632f\u308b\u821e\u3044<\/strong><\/a><br \/>\n\u7b2c2\u56de\uff1a<a href=\"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/2021\/12\/javascript-call-apply.html\"><strong>this\u306e\u53c2\u7167 -call\u30fbapply<\/strong><\/a><br \/>\n\u7b2c3\u56de\uff1a<a href=\"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/2021\/12\/javascript-bind.html\"><strong>this\u306e\u675f\u7e1b -bind<\/strong><\/a>\uff08\u5f53\u8a18\u4e8b\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"this\u306e\u5024\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308bbind\u30e1\u30bd\u30c3\u30c9\u3002 bind\u3068\u3044\u3046\u540d\u524d\u306e\u901a\u308a\u3001this\u3092\u675f\u7e1b\u3059\u308b\u305f\u3081\u306b\u5229\u7528\u3055\u308c\u307e\u3059\u3002 \u4eca\u56de\u306f\u3001this\u3092\u81ea\u7531\u306b\u5236\u5fa1\u3059\u308b\u305f\u3081\u306b\u4fbf\u5229\u306abind\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u89e3\u8aac\u3068\u3001\u985e\u4f3c\u306ecall\/appl [&hellip;]","protected":false},"author":1,"featured_media":117717,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1144],"tags":[964,1098],"class_list":["post-116747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-application","tag-javascript","tag-bind"],"_links":{"self":[{"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/posts\/116747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/comments?post=116747"}],"version-history":[{"count":0,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/posts\/116747\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/media\/117717"}],"wp:attachment":[{"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/media?parent=116747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/categories?post=116747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/tags?post=116747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}