{"id":60,"date":"2015-01-18T09:20:32","date_gmt":"2015-01-18T09:20:32","guid":{"rendered":"http:\/\/www.mbarsinai.com\/blog\/?p=60"},"modified":"2015-02-21T21:11:06","modified_gmt":"2015-02-21T21:11:06","slug":"uninheritance-using-undefined-in-javascript","status":"publish","type":"post","link":"https:\/\/www.mbarsinai.com\/blog\/2015\/01\/18\/uninheritance-using-undefined-in-javascript\/","title":{"rendered":"Uninheritance Using Undefined in Javascript"},"content":{"rendered":"<p>Javascript has a value called <code>undefined<\/code>. It&#8217;s an interesting little fella, which is the value of everything that is not defined. One common use of it is trolling your javascript console:<\/p>\n<p><code><br \/>\n> defined<br \/>\nReferenceError: defined is not defined<br \/>\n> var o = {}<br \/>\nundefined<br \/>\n> o.defined == undefined<br \/>\ntrue<br \/>\n<\/code><\/p>\n<p>Ha ha, silly javascript, not can&#8217;t even tell defined from it&#8217;s opposite. Except that it can. And it&#8217;s also very elegant.<\/p>\n<p>See, unlike other dynamic languages that requires programs to test the symbol table for symbol presence (e.g. ruby&#8217;s <code>defined?()<\/code> method), javascript does not have to test the table, since every valid expression (such as a variable reference) has a value. It just may be that this is <code>undefined<\/code>.<\/p>\n<p>Common inheritance models allows programmers to refine behavior (when overriding methods) or to extend them (when adding new methods). But javascript, with it&#8217;s <code>undefined<\/code> and its prototype-based inheritance, has a new type of inheritance &#8211; <strong>inheritance with method deletion<\/strong>. <\/p>\n<p>Suppose we want to model vehicles. Vehicles can go, er, I mean <code>go()<\/code>, and they also have a number of doors. Easy enough, here&#8217;s the vehicle object, and a car and a truck.<\/p>\n<p><code><br \/>\nvar vehicle = Object.create({});<br \/>\nvehicle.go = function() { return this.name + \" is going\"; };<br \/>\nvehicle.doors = function() { return this.doorCount; };<br \/>\n\/\/ make a truck<br \/>\nvar truck = Object.create( vehicle );<br \/>\ntruck.doorCount = 2;<br \/>\ntruck.name=\"Truck\";<br \/>\n\/\/ make a car<br \/>\nvar car = Object.create( vehicle );<br \/>\ncar.doorCount = 5;<br \/>\ncar.name=\"Car\";<br \/>\n<\/code><\/p>\n<p>This works fine (all examples here were executed using <a href=\"https:\/\/www.mozilla.org\/en-US\/firefox\/developer\/\" target=\"_blank\">Firefox Developer Edition<\/a>):<br \/>\n<code><br \/>\n> var aCar = Object.create(car);<br \/>\nundefined<br \/>\n> aCar.name=\"MyLittlePony\";<br \/>\n\"MyLittlePony\"<br \/>\n> aCar.go()<br \/>\n\"MyLittlePony is going\"<br \/>\n<\/code><\/p>\n<p>Nothing new so far. But how do you go about modeling a broken car? When using languages that use class-based inheritance, such as Java, one might result to throwing an <code>IllegalStateException<\/code>, since <code>brokenCar<\/code> is a <code>vehicle<\/code> and thus must have a <code>go()<\/code> method. In javascript, however, there&#8217;s another option:<\/p>\n<p><code><br \/>\n> var brokenCar = Object.create( car )<br \/>\nundefined<br \/>\n> brokenCar.go = undefined;<br \/>\n<\/code><\/p>\n<p>And now:<br \/>\n<code><br \/>\n> brokenCar.doors()<br \/>\n5<br \/>\n<\/code><br \/>\nBut<br \/>\n<code><br \/>\n> brokenCar.go()<br \/>\nTypeError: brokenCar.go is not a function<br \/>\n<\/code><\/p>\n<p>In other words, <code>brokenCar<\/code> is a <code>car<\/code> that can&#8217;t go, just like in real life, where a broken car is a car that can&#8217;t go.<\/p>\n<p>Achievement &#8220;modeling bliss&#8221; unlocked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript has a value called undefined. It&#8217;s an interesting little fella, which is the value of everything that is not defined. One common use of it is trolling your javascript console: > defined ReferenceError: defined is not defined > var o = {} undefined > o.defined == undefined true Ha ha, silly javascript, not can&#8217;t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3],"tags":[10,9],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript","tag-modeling"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3NnQg-Y","_links":{"self":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":6,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts\/60\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}