{"id":4881,"date":"2023-09-29T11:10:27","date_gmt":"2023-09-29T02:10:27","guid":{"rendered":"http:\/\/attacktube.com\/?p=4881"},"modified":"2023-11-05T11:25:03","modified_gmt":"2023-11-05T02:25:03","slug":"%e3%80%90javascript%e3%80%91object-create","status":"publish","type":"post","link":"https:\/\/attacktube.com\/?p=4881","title":{"rendered":"\u3010JavaScript\u3011Object.create()\u3010\u4e2d\u7d1a\u8005\u3011"},"content":{"rendered":"\n<p>Object.create\u306f\u3001\u65b0\u3057\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3057\u305f\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u751f\u6210\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b\u3002<br>\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u65b0\u3057\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u6307\u5b9a\u3057\u305f\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u5171\u6709\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<br>\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-3461056110605997\" crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3461056110605997\" data-ad-slot=\"3137443461\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<br>\n\n\n\n<p>\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u3068\u3044\u3046\u8a00\u8449\u306f\u901a\u5e38\u3001\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u3059\u3002\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u4ed6\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u7d99\u627f\u3059\u308b\u5171\u901a\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u4fdd\u6301\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3042\u308b\u3002\u3053\u308c\u3089\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3084\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u7d99\u627f\u5143\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30c1\u30a7\u30fc\u30f3\u3092\u901a\u3058\u3066\u30a2\u30af\u30bb\u30b9\u53ef\u80fd\u306b\u306a\u308b\u3002<br><br>Object.create()\u3067\u65b0\u3057\u304f\u751f\u6210\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068Deep copy(\u30c7\u30a3\u30fc\u30d7\u30b3\u30d4\u30fc)\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u4f3c\u3066\u3044\u308b\u3051\u3069\u6319\u52d5\u304c\u82e5\u5e72\u9055\u3046\u3002<br>\u3053\u308c\u306f\u3001\u307e\u305f\u4eca\u5ea6\u52c9\u5f37\u3057\u3088\u3002\u4f7f\u3044\u5206\u3051\u304c\u3044\u307e\u3044\u3061\u5206\u304b\u3089\u306a\u3044\u3002<br>Object.create()\u3067\u65b0\u3057\u304f\u751f\u6210\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068Deep copy(\u30c7\u30a3\u30fc\u30d7\u30b3\u30d4\u30fc)\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068Shallow copy(\u30b7\u30e3\u30ed\u30fc\u30b3\u30d4\u30fc)\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4f7f\u3044\u5206\u3051\u306f\uff1f\u3053\u306e\u8fba\u306e\u4f7f\u3044\u5206\u3051\u304c\u4e0d\u660e\u3002<\/p>\n\n\n\n<pre><code>\nconst protoObj = { \n  sayHello: function() {\n    console.log(&#039;Hello from prototype!&#039;);\n  }\n};\n\nconst newObj = Object.create(protoObj);\n\nnewObj.sayHello = function() {\n  console.log(&#039;Hello from newObj!&#039;);\n};\n\nprotoObj.sayHello();\/\/ Hello from prototype!\nnewObj.sayHello();\/\/ Hello from newObj!\n\nprotoObj.greeting = &#039;Greetings from prototype!&#039;;\nconsole.log(protoObj.greeting); \/\/ Greetings from prototype!\nconsole.log(newObj.greeting); \/\/ Greetings from prototype!\n<\/code><\/pre>\n\n\n\n<p>\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u3092\u5229\u7528\u3057\u305f\u7d99\u627f\u306e\u65b9\u6cd5\u306e1\u3064\u3068\u3057\u3066\u3001Object.create()\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u3042\u308b\u3002<br>Child.prototype = Object.create(Parent.prototype) \u306e\u5834\u5408\u3001Child.prototype \u306e\u5909\u66f4\u306f Parent.prototype \u306b\u5f71\u97ff\u3092\u4e0e\u3048\u306a\u3044\u3002<br>Child.prototype = Parent.prototype \u306e\u5834\u5408\u3001Child.prototype \u306e\u5909\u66f4\u304c Parent.prototype \u306b\u3082\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u3002<\/p>\n\n\n\n<pre><code>\nfunction Parent() {\n  this.parentProperty = &#039;I am a parent&#039;;\n}\n\nParent.prototype.parentMethod = function() {\n  console.log(&#039;Parent method&#039;);\n};\n\nfunction Child() {\n  \/\/ Child\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u304c\u547c\u3070\u308c\u305f\u3068\u304d\u3001Parent\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3082\u547c\u3073\u51fa\u3059\u3002\n  Parent.call(this);\n  \n  this.childProperty = &#039;I am a child&#039;;\n}\n\nconsole.log(typeof Parent);\/\/ function\nconsole.log(typeof Parent.prototype);\/\/ object\n\n\/\/ Parent\u306e\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u3092Child\u306e\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u3068\u3057\u3066\u6307\u5b9a\nChild.prototype = Object.create(Parent.prototype);\n\n\/\/ \u3053\u306e\u5834\u5408\u3001Child.prototype\u5909\u66f4\u3059\u308b\u3068Parent.prototype\u3082\u5909\u66f4\u3055\u308c\u308b\u3002\n\/\/Child.prototype = Parent.prototype;\n\nChild.prototype.childMethod = function() {\n  console.log(&#039;Child method&#039;);\n};\n\nconst childInstance = new Child();\nchildInstance.childMethod();  \/\/ Child method\nchildInstance.parentMethod();  \/\/ Parent method\n\nChild.prototype.anotherMethod = function() {\n  console.log(&#039;Another method&#039;);\n};\n\nconst childInstance2 = new Child();\nchildInstance2.anotherMethod();  \/\/ Another method\n\nconst parentInstance2 = new Parent();\nparentInstance2.anotherMethod();  \/\/ Uncaught TypeError: parentInstance2.anotherMethod is not a function\n<\/code><\/pre>\n\n\n\n<script type=\"text\/javascript\">\njQuery(function($){\n$(\".jp-video\").hide();\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Object.create\u306f\u3001\u65b0\u3057\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3057\u305f\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u751f\u6210\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b\u3002\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u65b0\u3057\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u6307\u5b9a\u3057\u305f\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u5171 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4881","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/4881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4881"}],"version-history":[{"count":13,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/4881\/revisions"}],"predecessor-version":[{"id":5012,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/4881\/revisions\/5012"}],"wp:attachment":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}