{"id":116742,"date":"2021-12-28T06:00:25","date_gmt":"2021-12-27T21:00:25","guid":{"rendered":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/?p=116742"},"modified":"2022-05-26T22:13:59","modified_gmt":"2022-05-26T13:13:59","slug":"javascript-call-apply","status":"publish","type":"post","link":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/2021\/12\/javascript-call-apply.html","title":{"rendered":"\u3010JavaScript\u306e\u5fdc\u7528\u3011this\u306e\u53c2\u7167 -call\u30fbapply"},"content":{"rendered":"<p>JavaScript\u306b\u304a\u3051\u308bthis\u306e\u5024\u306f\u3001\u57fa\u672c\u7684\u306b\u95a2\u6570\u306e\u5b9f\u884c\u6642\u306b\u6c7a\u5b9a\u3057\u307e\u3059\u3002<br \/>\n\u3057\u304b\u3057\u3001call\u30e1\u30bd\u30c3\u30c9\u3084apply\u30e1\u30bd\u30c3\u30c9\u3068\u4f7f\u3046\u3053\u3068\u3067\u3001\u95a2\u6570\u5b9f\u884c\u6642\u306b\u304a\u3044\u3066this\u306e\u53c2\u7167\u5148\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001this\u3092\u5236\u5fa1\u3059\u308b\u305f\u3081\u306ecall\/apply\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2 class=\"style2c\">call\/apply\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>call\/apply\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u95a2\u6570\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u306f\u3001\u3053\u308c\u3089\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u4f7f\u3046\u3068\u3001\u3069\u306e\u3088\u3046\u306a\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\n\u305d\u308c\u306f\u3001\u660e\u793a\u7684\u306bthis\u306e\u53c2\u7167\u5148\u3092\u6307\u5b9a\u3057\u3066\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u3068\u3044\u3046\u70b9\u3067\u3059\u3002\u4e00\u8a00\u3067\u8a00\u3048\u3070\u3001\u5143\u306ethis\u306e\u5024\u3092\u7121\u7406\u3084\u308a\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<h2 class=\"style2c\">call\/apply\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u3044\u65b9<\/h2>\n<p>\u8aac\u660e\u3060\u3051\u3067\u306f\u7406\u89e3\u3057\u306b\u304f\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u306e\u3067\u3001\u4f8b\u3092\u53c2\u8003\u306b\u3057\u306a\u304c\u3089\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3 class=\"style3c\">\u57fa\u672c\u7684\u306a\u632f\u308b\u821e\u3044<\/h3>\n<p>\u307e\u305a\u306f\u3001\u57fa\u672c\u7684\u306a\u632f\u308b\u821e\u3044\u3092\u898b\u3066\u307f\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306f\u3001test\u3068\u3044\u3046\u95a2\u6570\u3092\u5358\u7d14\u306b\u547c\u3073\u51fa\u3059\u30d1\u30bf\u30fc\u30f3\u3068\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u305f\u30d1\u30bf\u30fc\u30f3\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>function test() {\r\n  console.log('test');\r\n}\r\n\r\ntest(); \/\/ \"test\"\r\ntest.call(); \/\/ \"test\"\r\ntest.apply(); \/\/ \"test\"\r\n<\/code><\/pre>\n<\/div>\n<p>\u3059\u3079\u3066\u540c\u3058\u7d50\u679c\u3068\u306a\u308a\u307e\u3057\u305f\u3002\u8a00\u3044\u63db\u3048\u308b\u3068\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u95a2\u6570test\u81ea\u8eab\u3092\u6307\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u51fa\u529b\u5148\u306bthis\u3092\u6307\u5b9a\u3057\u3001\u540c\u3058\u3088\u3046\u306b\u95a2\u6570\u3068\u30e1\u30bd\u30c3\u30c9\u306e\u547c\u3073\u51fa\u3057\u3092\u884c\u3046\u3068\u3069\u3046\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>function test() {\r\n  console.log(this);\r\n}\r\n\r\ntest(); \/\/ Window\r\ntest.call(); \/\/ Window\r\ntest.apply(); \/\/ Window\r\n<\/code><\/pre>\n<\/div>\n<p>\u975estrict\u30e2\u30fc\u30c9\u4e0b\u306ethis\u306f\u3001\u3059\u3079\u3066\u306e\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u30d1\u30bf\u30fc\u30f3\u306b\u304a\u3044\u3066Window\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53c2\u7167\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3057\u304b\u3057\u3001\u3053\u308c\u3060\u3051\u3067\u306f\u3001\u5358\u7d14\u306b\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3060\u3051\u3067this\u306e\u5024\u3092\u5236\u5fa1\u3057\u3066\u3044\u308b\u3068\u306f\u8a00\u3048\u307e\u305b\u3093\u3002<\/p>\n<p>\u3053\u3053\u3067call\/apply\u306e\u69cb\u6587\u3068\u5f79\u5272\u3092\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3 class=\"style3c\">call\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u69cb\u6587<\/h3>\n<p>\u4ee5\u4e0b\u306f\u3001call\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u69cb\u6587\u3067\u3059\u3002\u7b2c\u4e00\u5f15\u6570\u306bthis\u306e\u5024\u3092\u6307\u5b9a\u3057\u3001\u7b2c\u4e8c\u5f15\u6570\u4ee5\u964d\u306b\u306f\u547c\u3073\u51fa\u3059\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.call(this\u306e\u5024, ...\u95a2\u6570\u306e\u5f15\u6570);\r\n<\/code><\/pre>\n<\/div>\n<h3 class=\"style3c\">apply\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u69cb\u6587<\/h3>\n<p>\u4ee5\u4e0b\u306f\u3001apply\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u69cb\u6587\u3067\u3059\u3002\u7b2c\u4e00\u5f15\u6570\u306bthis\u306e\u5024\u3092\u6307\u5b9a\u3057\u3001\u7b2c\u4e8c\u5f15\u6570\u306b\u306f\u547c\u3073\u51fa\u3059\u95a2\u6570\u306e\u5f15\u6570\u3092\u914d\u5217\u3068\u3057\u3066\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.apply(this\u306e\u5024, [\u95a2\u6570\u306e\u5f15\u6570]);\r\n<\/code><\/pre>\n<\/div>\n<h3 class=\"style3c\">call\/apply\u30e1\u30bd\u30c3\u30c9\u306e\u5f79\u5272<\/h3>\n<p>\u6b21\u306b\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u306b\u5f15\u6570\u3092\u5165\u308c\u3066\u52d5\u304d\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>let user = {name: 'World'};\r\n\r\nfunction greeting(message) {\r\n  return `${message}, ${this.name}!`;\r\n}\r\n\r\n\/\/ call\u30e1\u30bd\u30c3\u30c9\r\nconst call = greeting.call(user, 'Hello');\r\nconsole.log(call); \/\/ \"Hello World!\"\r\n\r\n\/\/ apply\u30e1\u30bd\u30c3\u30c9\r\nconst apply = greeting.apply(user, ['Nice to meet you']);\r\nconsole.log(apply); \/\/ \"Nice to meet you, World!\"\r\n<\/code><\/pre>\n<\/div>\n<p>call\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u3001this\u306e\u5024\u3068\u3057\u3066user\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3057\u3001\u95a2\u6570greeting\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001\u7b2c\u4e8c\u5f15\u6570\u306b\u6307\u5b9a\u3057\u305f<code>'Hello'<\/code>\u304c\u95a2\u6570greeting\u306e\u5f15\u6570message\u306b\u5165\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>apply\u30e1\u30bd\u30c3\u30c9\u3082\u540c\u69d8\u3067\u3059\u3002<br \/>\napply\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u3001this\u306e\u5024\u3068\u3057\u3066user\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3057\u305f\u72b6\u614b\u3067\u95a2\u6570greeting\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001apply\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e8c\u5f15\u6570\u306b\u6307\u5b9a\u3057\u305f\u914d\u5217<code>['Nice to meet you']<\/code>\u306f\u3001\u95a2\u6570\u304c\u547c\u3070\u308c\u308b\u3068\u81ea\u52d5\u7684\u306b\u4e2d\u8eab\u304c\u5c55\u958b\u3055\u308c\u3001\u95a2\u6570greeting\u306e\u5f15\u6570message\u306b\u5165\u308a\u307e\u3059\u3002<\/p>\n<p>this\u304c\u5fc5\u8981\u306a\u3044\u5834\u5408\u306b\u306f\u3001\u7b2c\u4e00\u5f15\u6570\u306bnull\u3092\u6e21\u3059\u306e\u304c\u4e00\u822c\u7684\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>function greeting(message) {\r\n  return message;\r\n}\r\n\r\nconst call = greeting.call(null, 'Hello');\r\nconsole.log(call); \/\/ \"Hello\"\r\n\r\nconst apply = greeting.apply(null, ['Nice to meet you']);\r\nconsole.log(apply); \/\/ \"Nice to meet you\"\r\n<\/code><\/pre>\n<\/div>\n<p>\u3044\u304b\u304c\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>call\/apply\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u901a\u5e38\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u5148\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3055\u308c\u308bthis\u306e\u5024\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u305d\u308c\u306b\u3088\u308a\u3001\u95a2\u6570\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u5225\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2 class=\"style2c\">call\/apply\u30e1\u30bd\u30c3\u30c9\u306e\u9055\u3044<\/h2>\n<p>call\/apply\u30e1\u30bd\u30c3\u30c9\u306f\u307b\u3068\u3093\u3069\u540c\u3058\u52d5\u304d\u3092\u3057\u307e\u3059\u304c\u3001\u9055\u3044\u3082\u3042\u308a\u307e\u3059\u3002<br \/>\n\u305d\u308c\u306f\u3001\u95a2\u6570\u306e\u5f15\u6570\u3078\u306e\u5024\u306e\u6e21\u3057\u65b9\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>let user = {name: '\u592a\u90ce'};\r\n\r\nfunction add(a, b) {\r\n  return `${this.name}\u306e\u30b9\u30b3\u30a2\u306e\u5408\u8a08\u306f${a + b}\u3067\u3059`;\r\n}\r\n\r\n\/\/ \u30d1\u30bf\u30fc\u30f31\r\nconsole.log(add.call(user, 10, 20)); \/\/ \"\u592a\u90ce\u306e\u30b9\u30b3\u30a2\u306e\u5408\u8a08\u306f30\u3067\u3059\"\r\nconsole.log(add.apply(user, [30, 40])); \/\/ \"\u592a\u90ce\u306e\u30b9\u30b3\u30a2\u306e\u5408\u8a08\u306f70\u3067\u3059\"\r\n\r\n\/\/ \u30d1\u30bf\u30fc\u30f32\r\nlet score1 = {a: 10, b: 20}\r\nlet score2 = [30, 40];\r\nconsole.log(add.call(user, score1.a, score1.b)); \/\/ \"\u592a\u90ce\u306e\u30b9\u30b3\u30a2\u306e\u5408\u8a08\u306f30\u3067\u3059\"\r\nconsole.log(add.apply(user, score2)); \/\/ \"\u592a\u90ce\u306e\u30b9\u30b3\u30a2\u306e\u5408\u8a08\u306f70\u3067\u3059\"\r\n<\/code><\/pre>\n<\/div>\n<p>call\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u7b2c\u4e8c\u5f15\u6570\u4ee5\u964d\u3092\u305d\u306e\u307e\u307e\u6307\u5b9a\u3059\u308b\u306e\u306b\u5bfe\u3057\u3001apply\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u7b2c\u4e8c\u5f15\u6570\u3092\u914d\u5217\u306b\u5165\u308c\u3066\u6307\u5b9a\u3059\u308b\u70b9\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2 class=\"style2c\">\u307e\u3068\u3081<\/h2>\n<p>\u4eca\u56de\u306f\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>this\u306e\u632f\u308b\u821e\u3044\u3092\u7406\u89e3\u3057\u3066\u3044\u308c\u3070this\u306e\u6319\u52d5\u3092\u4e88\u60f3\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3057\u304b\u3057\u3001this\u306e\u5024\u3092\u6307\u5b9a\u3057\u3066\u304a\u304d\u305f\u3044\u5834\u5408\u3084\u3001\u4e88\u671f\u3057\u306a\u3044this\u306e\u6319\u52d5\u3092\u767a\u751f\u3055\u305b\u306a\u3044\u305f\u3081\u306b\u3082\u3001call\/apply\u30e1\u30bd\u30c3\u30c9\u3092\u77e5\u3063\u3066\u304a\u304f\u3068\u5f79\u306b\u7acb\u3061\u307e\u3059\u3002<\/p>\n<p>\u521d\u5fc3\u8005\u304c\u3064\u307e\u3065\u304d\u3084\u3059\u3044\u3068\u8a00\u308f\u308c\u308bthis\u3067\u3059\u304c\u3001\u3053\u308c\u3089\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u306a\u304c\u3089\u7406\u89e3\u3092\u6df1\u3081\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>\uff08\u5f53\u8a18\u4e8b\uff09<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><\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u306b\u304a\u3051\u308bthis\u306e\u5024\u306f\u3001\u57fa\u672c\u7684\u306b\u95a2\u6570\u306e\u5b9f\u884c\u6642\u306b\u6c7a\u5b9a\u3057\u307e\u3059\u3002 \u3057\u304b\u3057\u3001call\u30e1\u30bd\u30c3\u30c9\u3084apply\u30e1\u30bd\u30c3\u30c9\u3068\u4f7f\u3046\u3053\u3068\u3067\u3001\u95a2\u6570\u5b9f\u884c\u6642\u306b\u304a\u3044\u3066this\u306e\u53c2\u7167\u5148\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002 \u4eca\u56de\u306f\u3001this\u3092\u5236 [&hellip;]","protected":false},"author":1,"featured_media":117715,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1144],"tags":[964],"class_list":["post-116742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-application","tag-javascript"],"_links":{"self":[{"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/posts\/116742","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=116742"}],"version-history":[{"count":0,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/posts\/116742\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/media\/117715"}],"wp:attachment":[{"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/media?parent=116742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/categories?post=116742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anthem00.xsrv.jp\/2023_new_tcd\/wp-json\/wp\/v2\/tags?post=116742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}