OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JavaScript class with static methods vs object with function properties

  • Thread starter Thread starter Cam Parry
  • Start date Start date
C

Cam Parry

Guest
In JavaScript (and TypeScript) is there any difference between using a class vs an object to namespace methods from a performance perspective? And are there any other reasons to prefer one option over the other?

Code:
export const UserService = {
  async findMany() {
    // ...
  },
  async findById(id: number) {
    // ...
  }
}

Code:
export class UserService {
  static async findMany() {
    // ...
  }
  static async findById(id: number) {
    // ...
  }
}

<p>In JavaScript (and TypeScript) is there any difference between using a class vs an object to namespace methods from a performance perspective? And are there any other reasons to prefer one option over the other?</p>
<pre><code>export const UserService = {
async findMany() {
// ...
},
async findById(id: number) {
// ...
}
}
</code></pre>
<pre><code>export class UserService {
static async findMany() {
// ...
}
static async findById(id: number) {
// ...
}
}


</code></pre>
 

Latest posts

Top