Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Last active October 13, 2017 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiebuilds/a8a7f7085ebe8566d6abb5351ce0d212 to your computer and use it in GitHub Desktop.
Save jamiebuilds/a8a7f7085ebe8566d6abb5351ce0d212 to your computer and use it in GitHub Desktop.

Namespaces

Note: This borrows a lot from TypeScript.

Basic

Input

namespace MyNamespace {
  function nonMember() {}
  
  export class Member {
    methodA() {
      nonMember(true);
    }
    
    methodB() {
      nonMember(false);
    }
  }
}

Output

const MyNamespace = {};

(() => {
  function nonMember() {}
  
  MyNamespace.Member = class Member {
    methodA() {
      nonMember(true);
    }
    
    methodB() {
      nonMember(false);
    }
  };
}());

Extension

Input

namespace MyNamespace {
  export class Foo {}
}

namespace MyNamespace {
  export class Bar {}
}

Output

const MyNamespace = {};

(() => {
  MyNamespace.Foo = class Foo {};
}());

(() => {
  MyNamespace.Bar = class Bar {};
}());

Nested

Input

namespace MyNamespace.Nested {
  export class Member {}
}

Output

const MyNamespace = {};

Namespace.Nested = {}

(() => {
  Namespace.Nested.Member = class Member {};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment