angularjs - TypeScript: Can't define class inside an IIFE -


i want define class inside iife using typescript , keeps getting error, class can defined either in module or file. below code not work.

(function(){     // error here     class person {      } })() 

the reason need because don't want expose global variables, not module. may wonder why, because add them angular modules in below way

(function(){     angular.module('app').controller('homecontroller', homecontroller);     // error here     class homecontroller {     } })(); 

when create class, generates iife you:

the class:

class example {  } 

results in javascript:

var example = (function () {     function example() {     }     return example; })(); 

if want keep classes out of global scope, can use modules:

module stack {     export class example {      }      export class overflow {      } } 

only stack module appears in global scope.

var stack; (function (stack) {     var example = (function () {         function example() {         }         return example;     })();     stack.example = example;     var overflow = (function () {         function overflow() {         }         return overflow;     })();     stack.overflow = overflow; })(stack || (stack = {})); 

if want go step further, can use external modules - when that, none of classes or modules added global scope.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -