{"id":1074,"date":"2022-08-30T15:11:57","date_gmt":"2022-08-30T15:11:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/in-javascript-where-do-prototypes-functions-hide-when-we-instantiate-the-derived-object-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:57","modified_gmt":"2022-08-30T15:11:57","slug":"in-javascript-where-do-prototypes-functions-hide-when-we-instantiate-the-derived-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/in-javascript-where-do-prototypes-functions-hide-when-we-instantiate-the-derived-object-collection-of-common-programming-errors\/","title":{"rendered":"In JavaScript, where do prototype&#39;s functions hide when we instantiate the derived object?-Collection of common programming errors"},"content":{"rendered":"<p>I am currently trying to figure out exactly how the prototypical inheritance works in JavaScript. Here is the mystery I currently trying to solve.<\/p>\n<p>Suppose we set up the following structure:<\/p>\n<pre><code>var base = { greet : \"hello\" }\n\nvar child = function () {}\n\nchild.prototype = base;\nchild.prototype.protoSomething = function () {}\n\nvar instance = new child();\n<\/code><\/pre>\n<p>Nothing fancy. Now lets see what <code>instance<\/code> has for properties (own or otherwise):<\/p>\n<pre><code>for(prop in instance) {\n    console.log(prop); \/\/ prints 'greet', 'protoSomething'\n}\n<\/code><\/pre>\n<p>Alright, so it has <code>greet<\/code> and <code>protoSomething<\/code>, are they <code>instance<\/code>&#8216;s own members?<\/p>\n<pre><code>for(prop in instance) {\n    if(instance.hasOwnProperty(prop))\n        console.log(prop); \/\/ nothing is printed\n}\n<\/code><\/pre>\n<p>Nope, own property list is empty. Are they in <code>instance<\/code>&#8216;s prototype?<\/p>\n<pre><code>if(instance.prototype === undefined) {\n    console.log(\"'instance' has no prototype\"); \/\/ gets printed\n}\n<\/code><\/pre>\n<p>Well, bummer, <code>instance<\/code> doesn&#8217;t have a prototype assigned. So, if the properties are not <em>own<\/em> and there is no prototype, where are they coming from? I feel like some sort of a diagram would be very nice at this point.<\/p>\n<ol>\n<li>\n<p>Only a function has the property <code>prototype<\/code>.<\/p>\n<pre><code>instance.constructor.prototype === base \/\/ return true\n<\/code><\/pre>\n<p>To get a prototype of an object, you should use the Object.getPrototypeOf method.<\/p>\n<pre><code>Object.getPrototypeOf(instance) === base \/\/ return true\n<\/code><\/pre>\n<\/li>\n<li>\n<p>This page should help you out:<\/p>\n<p>http:\/\/joost.zeekat.nl\/constructors-considered-mildly-confusing.html<\/p>\n<p>In short, the &#8216;behind the scenes&#8217; prototype of an instance (created via <code>new<\/code>) is not accessible in any way, hence why it returns <code>undefined<\/code>. Only the constructor (on which you&#8217;ve manually set a <code>.prototype<\/code> property) will return anything when you try to retrieve it.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:21:29. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am currently trying to figure out exactly how the prototypical inheritance works in JavaScript. Here is the mystery I currently trying to solve. Suppose we set up the following structure: var base = { greet : &#8220;hello&#8221; } var child = function () {} child.prototype = base; child.prototype.protoSomething = function () {} var instance [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1074","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1074"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1074\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}