
{"id":2235,"date":"2017-01-03T01:06:50","date_gmt":"2017-01-03T06:06:50","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2235"},"modified":"2017-01-03T01:06:50","modified_gmt":"2017-01-03T06:06:50","slug":"javascript-wtf-3-foo-prototype-is-not-prototype-of-foo","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2235","title":{"rendered":"JavaScript WTF #3: Foo.prototype is not prototype of Foo"},"content":{"rendered":"<p>Consider the following code:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction Foo() {}\r\nvar fooPrototype = Object.getPrototypeOf(Foo);\r\nconsole.log(Foo.prototype === fooPrototype); \/\/ false\r\n<\/pre>\n<p>Why is it false? Because <code>Foo.prototype<\/code> is in fact not the prototype of Foo, but the prototype of objects created via expression <code>new Foo()<\/code>:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar obj = new Foo();\r\nvar objPrototype = Object.getPrototypeOf(obj);\r\nconsole.log(Foo.prototype === objPrototype); \/\/ true\r\n<\/pre>\n<p>As for the actual prototype of Foo, it is <code>Function.prototype<\/code>:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.log(fooPrototype === Function.prototype); \/\/ true\r\n<\/pre>\n<p>This is <i>very<\/i> confusing for someone who is trying to learn about prototypes. Although JavaScript is built on prototypes, it is inexplicably shy about discovering and working with prototypes. To the best of my knowledge, <code>Object.getPrototypeOf()<\/code> was added only in EcmaScript 5.1, 14 years after JavaScript&#8217;s inception. For older versions of JavaScript it is recommended to use <code>obj.constructor.prototype<\/code> to retrieve prototype of <code>obj<\/code>. This is rather indirect, and it would fail is someone reassign the <code>constructor<\/code> property. <\/p>\n<p>To visualize how ridiculous this situation is, let&#8217;s mentally transfer it to another language. The concept of prototypes is as central to JavaScript as the concept of base (super) classes to object-oriented languages. A similar situation in Java would mean that:<\/p>\n<ul>\n<li>There is no keyword <code>super<\/code>.<\/li>\n<li>You can refer to the superclass portion of your object as <code>this.constructor.super<\/code>.<\/li>\n<li>There is a non-standard property <code>__super__<\/code> supported only on some JVMs, but not others.<\/li>\n<li>Method <code>Object.getSuperOf()<\/code> is finally added in Java 7.<\/li>\n<li>For any class C, expression <code>C.super<\/code> yields <code>Class.class<\/code>.<\/li>\n<p>This would make Java language a little bit of a mess, and we would ask WTF, wouldn&#8217;t we?<br \/>\n<a href=\"https:\/\/www.codeproject.com\" rel=\"tag\" style=\"display:none\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following code: function Foo() {} var fooPrototype = Object.getPrototypeOf(Foo); console.log(Foo.prototype === fooPrototype); \/\/ false Why is it false? Because Foo.prototype is in fact not the prototype of Foo, <a href=\"https:\/\/ikriv.com\/blog\/?p=2235\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":["entry","author-ikriv","post-2235","post","type-post","status-publish","format-standard","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2235"}],"version-history":[{"count":14,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2235\/revisions"}],"predecessor-version":[{"id":2249,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2235\/revisions\/2249"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}