{"id":3005,"date":"2014-06-10T00:53:29","date_gmt":"2014-06-10T00:53:29","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3005"},"modified":"2014-06-24T20:57:27","modified_gmt":"2014-06-24T20:57:27","slug":"javascript-prototypes-and-inheritance","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/javascript-prototypes-and-inheritance\/","title":{"rendered":"JavaScript Prototypes and Inheritance"},"content":{"rendered":"<p>Objects have a built-in attribute called prototype who&#8217;s value is a pointer to another object, and can be declared when creating a new object with <span class=\"code\">Object.create(<em>myPrototypeReference<\/em>)<\/span>. If an object is created using the convenience\u00a0of an object literal <span class=\"code\">var <em>newObject<\/em> = {<em>property<\/em>: &#8216;<em>value<\/em>&#8216;}<\/span> then the prototype is defaulted to\u00a0Object.prototype.<\/p>\n<h2>Overview<\/h2>\n<ul>\n<li>Objects are pairs of keys and values called properties (in Ruby, this structure is called a Hash; in Python, it\u2019s called a dictionary)<\/li>\n<li>Properties can be enumerable, configurable, and writable<\/li>\n<li><strong>Objects have a built-in\u00a0attribute called prototype<\/strong> who&#8217;s value is a pointer to another object\n<ul>\n<li>Specify an object&#8217;s prototype by using Object.create(<em>prototype<\/em>) when creating the new object<\/li>\n<li>If you don&#8217;t need a prototype then you can use the convenience of the <strong>object literal<\/strong> syntax instead &#8230; var <em>newObject<\/em> = {<em>property<\/em>: &#8216;<em>value<\/em>&#8216;} &#8230; which always set the newly created object&#8217;s prototype to an object located at Object.prototype<\/li>\n<\/ul>\n<\/li>\n<li>You can look up an object\u2019s prototype by using Object.getPrototypeOf(<em>object<\/em>)<\/li>\n<li>Define new properties with Object.defineProperty(<em>object<\/em>, <em>&#8216;newKey&#8217;<\/em>, {value:<em>&#8216;New Key Value&#8217;<\/em>, writable:true, enumerable:true, configurable:true})\n<ul>\n<li>Since creating a new writable, configurable, enumerable property is pretty common, JavaScript makes it easy to do so using assignment syntax instead &#8230;\u00a0<em>object.newKey<\/em>\u00a0= &#8216;<em>newValue&#8217;<\/em><\/li>\n<\/ul>\n<\/li>\n<li><strong>Prototypes<\/strong> can be used to <strong>inherit<\/strong> functionality<\/li>\n<li>If you try to look up a key on an object and it is not found, JavaScript will look for it in the prototype. It will follow the <strong>&#8220;prototype chain&#8221;<\/strong> until it sees a null value. In that case, it returns undefined.<\/li>\n<\/ul>\n<h2>Prototypal Inheritance Analogy<\/h2>\n<pre>var defaults = {\r\n    zero: 0,\r\n    one: 1\r\n};\r\n\r\nvar myOptions = Object.create(defaults);\r\nvar yourOptions = Object.create(defaults);\r\n\r\n\/\/ When I want to change *just* my options\r\nmyOptions.zero = 1000;\r\n\r\n\/\/ When you wanna change yours\r\nyourOptions.one = 42;\r\n\r\n\/\/ When we wanna change the **defaults** even after we've got our options\r\n\/\/ even **AFTER** we've already created our instances\r\ndefaults.two = 2;\r\n\r\nmyOptions.two; \/\/ 2\r\nyourOptions.two; \/\/ 2\r\n<\/pre>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/yehudakatz.com\/2011\/08\/12\/understanding-prototypes-in-javascript\/\" target=\"_blank\">Understanding Prototypes in JavaScript<\/a><\/li>\n<li><a href=\"https:\/\/alexsexton.com\/blog\/2013\/04\/understanding-javascript-inheritance\/\" target=\"_blank\">Understanding JavaScript Inheritance\/<\/a><\/li>\n<li><a href=\"http:\/\/javascript.crockford.com\/prototypal.html\" target=\"_blank\">Prototypal Inheritance in JavaScript<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Objects have a built-in attribute called prototype who&#8217;s value is a pointer to another object, and can be declared when creating a new object with Object.create(myPrototypeReference). If an object is created using the convenience\u00a0of an object literal var newObject = {property: &#8216;value&#8216;} then the prototype is defaulted to\u00a0Object.prototype. Overview Objects are pairs of keys and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[9],"class_list":["post-3005","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-javascript"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3005"}],"version-history":[{"count":14,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3005\/revisions"}],"predecessor-version":[{"id":3331,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3005\/revisions\/3331"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}