{"id":3781,"date":"2016-04-18T01:45:31","date_gmt":"2016-04-18T01:45:31","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3781"},"modified":"2016-04-18T01:45:31","modified_gmt":"2016-04-18T01:45:31","slug":"typescript-publicprotectedprivate-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-publicprotectedprivate-example\/","title":{"rendered":"TypeScript public\/protected\/private example"},"content":{"rendered":"<p>TypeScript lets you do inheritance in Javascript, but the restrictions are enforced at compile time, rather than the typical Javascript tendency to randomly throw errors whenever the browser feels like it.<\/p>\n<p>Part of this is enforcement of access specification:<\/p>\n<pre lang=\"javascript\">\nclass Dialog {\n    message: String;\n   \n    render() {\n    }\n \n    public e() {\n        console.log(\"e\");   \n    }\n    \n    protected f() {\n        console.log(\"f\");\n    }\n    \n    private g() {\n        console.log(\"g\");\n    }\n}\n\nclass AlertDialog extends Dialog {\n     render() {\n         this.e();\n         this.f();\n     }    \n\n     public e() {\n          super.e();\n         console.log(\"e - child\"); \n     }     \n     protected f() {\n         console.log(\"f - child\");\n     }\n}\n\nlet ad: Dialog = new AlertDialog();\nad.render();\n<\/pre>\n<p>This prints out the following:<\/p>\n<pre>\ne\ne - child\nf - child\n<\/pre>\n<p>From this you can see that you can&#8217;t call private methods outside their own class, and protected methods are automatically virtual methods that override the parents. Public is available as an access specifier for completeness, but not necessary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Demonstration of how to do access specification on TypeScript classes<\/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\/3781"}],"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=3781"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}