{"id":3775,"date":"2016-04-18T01:36:20","date_gmt":"2016-04-18T01:36:20","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3775"},"modified":"2016-04-18T01:36:20","modified_gmt":"2016-04-18T01:36:20","slug":"typescript-inheritance-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-inheritance-example\/","title":{"rendered":"TypeScript Inheritance Example"},"content":{"rendered":"<p>The following shows an example of inheritance in TypeScript. This resembles languages like Java and C#, but comes with peculiarities that are specific to this language.<\/p>\n<pre lang=\"javascript\">\nclass Dialog {\n    message: String;\n   \n    constructor(message: String) {\n        this.message = message;    \n    }\n     \n    render() {\n        console.log(this.message); \n    }\n}\n\nclass AlertDialog extends Dialog {\n     render() {\n         console.log(\"Alert\"); \n     }    \n}\n\nlet ad: Dialog = new AlertDialog(\"alert message\");\nad.render();\n<\/pre>\n<p>One thing I really like is that the child classes inherit the constructor from the top class, if one isn&#8217;t defined. If you do want to define one, call super:<\/p>\n<pre lang=\"javascript\">\nconstructor() {\n  super(\"Alert message\")    \n}\n<\/pre>\n<p>By default, all methods are public, but you can define them as protected or private, and they act as expected, although the access level is erased (I haven&#8217;t found anything like protected internal or final)<\/p>\n<p>One thing that is also really nice is that the actual Javascript that gets interpreted from TypeScript is not that complex. Here is what it ends up looking like:<\/p>\n<pre lang=\"javascript\">\nvar __extends = (this && this.__extends) || function (d, b) {\n    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n    function __() { this.constructor = d; }\n    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};\nvar Dialog = (function () {\n    function Dialog(message) {\n        this.title = \"Test Title\";\n        this.message = message;\n    }\n    Dialog.prototype.render = function () {\n        console.log(this.title);\n        console.log(this.message);\n    };\n    return Dialog;\n}());\nvar d = new Dialog(\"Test Message\");\nd.render();\nvar AlertDialog = (function (_super) {\n    __extends(AlertDialog, _super);\n    function AlertDialog() {\n        _super.apply(this, arguments);\n    }\n    AlertDialog.prototype.render = function () {\n        console.log(\"Alert\");\n    };\n    return AlertDialog;\n}(Dialog));\nvar ad = new AlertDialog(\"alert message\");\nad.render();\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Demonstration of how inheritance gets rendered from TypeScript to Javascript<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,557],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3775"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3775"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3775\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}