Logo

dev-resources.site

for different kinds of informations.

TypeScript: Namespace VS Class

Published at
12/20/2023
Categories
typescript
class
oop
coding
Author
pierre
Categories
4 categories in total
typescript
open
class
open
oop
open
coding
open
Author
6 person written this
pierre
open
TypeScript: Namespace VS Class

TypeScript: Would you better wrap functions in class or namespace?

In this article, we will see if it is more judicious to store functions in a namespace or a class, based on the size and complexity of the application.

// namespace
export namespace UserModel {
  type uuid = string;

  interface User {
    id: uuid;
    username: string;
    firstName: string;
    lastName: string;
    password: string;
    sex: string;
    age: number;
  }

  const users = new Map();

  export function createUser(user: User) {
    const userKey = getUserKey(user.id);
    users.set(userKey, user);
  }

  function getUserKey(id: uuid) {
    return `id${id}`;
  }
}

// usage
UserModel.createUser(user);
Enter fullscreen mode Exit fullscreen mode
// class
export class UserModel {
  // ...
  public createUser(user: User) {
    const userKey = this.getUserKey(user.id);
    users.set(userKey, user);
  }

  private getUserKey(id: uuid) {
    return `id${id}`;
  }
}

// usage
const userModel = new UserModel();
userModel. createUser(user);
Enter fullscreen mode Exit fullscreen mode

As you can see in the above code, there are two different ways to organize and encapsulate your code within a scope, namely, within a traditional class or a simple namespace.

Although adding your code to a namespace instead of a class will not bring any of the OOP features and advantages you could have with a class, it still allows for elegant and simple usage of your code while isolating it from your application's global scope.

Calling another function within the same namespace is exactly like calling any other function, whereas calling a function within a class requires using the this keyword, and each function declaration is specified by its access modifiers.

In term of readability and avoid conflicts with identical functions, declaring namespaces can be an excellent technique in small projects.


I can't wait to hear from your feedback / thoughts in comments! 🀠

class Article's
30 articles in total
Favicon
Day3 Class and object in java:
Favicon
Day2 java program
Favicon
Day 6 - Object & Class
Favicon
Unflatten in PyTorch
Favicon
Day 4 java class
Favicon
Get 10th Class Math Notes (Matric Part 2) – Download Now for Free
Favicon
Day 18 - Object Oriented Programming
Favicon
Flatten in PyTorch
Favicon
Elevate Learning with Our E-Class: The Ultimate Student Performance Tracking Software
Favicon
Elevate Learning with Class Connect Pro: The Ultimate Student Performance Tracking Software
Favicon
ExtendableError usage in changesets errors package
Favicon
WordPress Training In Hyderabad
Favicon
Understanding the Distinction Between Class and Object in Object-Oriented Programming
Favicon
OOP concepts are importants
Favicon
Class : Inheritanceβœ… | Struct : Inheritance ❌
Favicon
One JS Class to speak them all
Favicon
Classes in C# | Uzbek
Favicon
Javascript classes explanation in a simple way
Favicon
Unveiling the Power of the :empty() CSS Pseudo-Class
Favicon
Mastering the :not() CSS Pseudo-Class
Favicon
Exploring the :has() CSS Pseudo-Class
Favicon
Unveiling the Java Superhero: The Mysterious Object Class
Favicon
How to use functions in react class components?
Favicon
Safeguarding the Seas: The Strategic Importance of Virginia-Class Submarines
Favicon
Mastering Metaclasses in Python using real-life scenarios
Favicon
TypeScript: Namespace VS Class
Favicon
Understanding Object-Oriented Programming: Unveiling the Power of Classes
Favicon
Learning Python classes
Favicon
Java Polymorphism Best Practices: Writing Clean and Extensible Code
Favicon
This Is How I Prepare The Very Best Regularization / Classification Images Dataset For Stable Diffusion

Featured ones: