最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6 class static method vs function - Stack Overflow

programmeradmin1浏览0评论

I am using NodeJS and going to write some utility functions. I think of two options here.

The first one is the traditional approach, i.e.

module.exports = {
    random: () => Math.random(),
};

And the second option is to use ES6 class with static methods, e.g.

class MyMath {
    static random() {
        return Math.random();
    }
}
module.exports = MyMath;

From programming/unit testing's perspective which one is better? Or they are pretty much the same because ES6 class essentially is a syntactic sugar?

Update: Thanks for the people who mented. I saw those questions asking class static method v.s. instance method, or prototype methods v.s. object method but mine is more like class static method v.s. Object method.

I am using NodeJS and going to write some utility functions. I think of two options here.

The first one is the traditional approach, i.e.

module.exports = {
    random: () => Math.random(),
};

And the second option is to use ES6 class with static methods, e.g.

class MyMath {
    static random() {
        return Math.random();
    }
}
module.exports = MyMath;

From programming/unit testing's perspective which one is better? Or they are pretty much the same because ES6 class essentially is a syntactic sugar?

Update: Thanks for the people who mented. I saw those questions asking class static method v.s. instance method, or prototype methods v.s. object method but mine is more like class static method v.s. Object method.

Share Improve this question edited Mar 8, 2019 at 6:15 Eric Xin Zhang asked Mar 8, 2019 at 5:54 Eric Xin ZhangEric Xin Zhang 1,34916 silver badges23 bronze badges 7
  • There is a great article by Cristian Salcescu called Classes vs Factory function: exploring the way forward – cross19xx Commented Mar 8, 2019 at 5:57
  • lol I am at my max flags for today, but this is a duplicate of: stackoverflow./questions/30783217/… – Steven Stark Commented Mar 8, 2019 at 5:57
  • 2 Possible duplicate of Why should I use ES6 classes? – cross19xx Commented Mar 8, 2019 at 5:58
  • @cr05s19xx thanks for the link. I think it's a bit different. In the first option I am not using a construction function. – Eric Xin Zhang Commented Mar 8, 2019 at 6:06
  • I think the dupe proposal does cover a lot of it but I do think this one is separate because of the focus on static members. The dupe only mentions that in passing in a single place in a single answer. So I see a valid reason to have somebody go more in-depth in when and why you'd use an ES6+ class with static members as opposed to a plain old object. – VLAZ Commented Mar 8, 2019 at 6:06
 |  Show 2 more ments

3 Answers 3

Reset to default 5

Using static-only classes as namespaces in JavaScript is the remnant of languages where a class is the only available entity. Modules already act as namespaces.

In case singleton object is needed, object literal should be used:

module.exports = {
    random: () => Math.random(),
};

There's already exports object that can be used:

exports.random = () => Math.random();

Or with ES modules:

export const random = () => Math.random();

With the class syntax you might give more than you really intended, it is a constructor that can be invoked with new, while with the object literal syntax you give a non-function object. As that is really what you want to expose, go for that.

Classes with only static methods are the same as a bunch of isolated functions. In this case you probably don't need to use it. As for unit tests it is pretty much the same since classes with only static methods does not differ that much from functions.

发布评论

评论列表(0)

  1. 暂无评论