{"id":3769,"date":"2020-05-24T15:43:58","date_gmt":"2020-05-24T15:43:58","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3769"},"modified":"2020-05-25T13:36:39","modified_gmt":"2020-05-25T13:36:39","slug":"modules-basics","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/modules-basics\/","title":{"rendered":"Modules Basics"},"content":{"rendered":"<p>Modules helps in writing clean code like seperating modules for dataacces, UI and security. Each modules has a separate role of its own<br \/>\nlike httpModule, routing module. Writing Modules in TS would create IIFE(Immediately Invoked Function Expression) in javascript file<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule dataService{\r\n\r\n}\r\n<\/pre>\n<p>So what is difference between class and module. Both are same except one. Classes are created in Global namespace.<br \/>\nHowever module can be either global or local.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass dataService{\r\n\r\n}\r\n<\/pre>\n<p>Now lets wrap class with in module which is nothing more than namespace<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule Shapes{\r\n  export class Rectangle{\r\n\r\n  } \r\n\r\n  export class Square{\r\n\r\n  } \r\n\r\n  export class Triangle{\r\n\r\n  } \r\n}\r\n\r\nvar objShapes:any = new Shapes.Square;\r\n<\/pre>\n<p><strong>Simple Program for Logging Message using Module and Interface<\/strong> <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ninterface ILoggerUtils {\r\n    print(): void;\r\n}\r\n\r\nvar LogLevel = {\r\n    info: 1,\r\n    Warn: 2,\r\n    Error:3\r\n}\r\n\r\nmodule LogUtils {\r\n    export class LoggerAtError implements ILoggerUtils {\r\n        \r\n    print(): void {\r\n        console.log(&quot;Log Message during Error&quot;);\r\n    }\r\n}\r\n\r\nexport class LoggerAtInfo implements ILoggerUtils {\r\n    print(): void {\r\n        console.log(&quot;Log Message during Info&quot;);\r\n    }\r\n}\r\n\r\nexport class LoggerAtWarn implements ILoggerUtils {\r\n    print(): void {\r\n        console.log(&quot;Log Message during Warn&quot;);\r\n    }\r\n  }\r\n}\r\n\r\nwindow.onload = function () {\r\n    var objLoggerUtil: ILoggerUtils;\r\n\r\n    var logLevel = LogLevel.info;\r\n\r\n    switch (logLevel) {\r\n        case LogLevel.info:\r\n            objLoggerUtil = new LogUtils.LoggerAtInfo();\r\n            break;\r\n        case LogLevel.Warn:\r\n            objLoggerUtil = new LogUtils.LoggerAtWarn();\r\n            break;\r\n        case LogLevel.Error:\r\n            objLoggerUtil = new LogUtils.LoggerAtError();\r\n            break;\r\n    }\r\n\r\n    objLoggerUtil.print();\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nLog Message during Info\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Modules helps in writing clean code like seperating modules for dataacces, UI and security. Each modules has a separate role of its own like httpModule, routing module. Writing Modules in TS would create IIFE(Immediately Invoked Function Expression) in javascript file module dataService{ } So what is difference between class and module. Both are same except&hellip; <a href=\"https:\/\/codethataint.com\/blog\/modules-basics\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[308],"tags":[],"class_list":["post-3769","post","type-post","status-publish","format-standard","hentry","category-modules"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3769"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3769\/revisions"}],"predecessor-version":[{"id":3772,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3769\/revisions\/3772"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}