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

javascript - How can I export static class methods without exporting the whole class - Stack Overflow

programmeradmin0浏览0评论

I am creating a node package to handle cookies. What is the best way to export static class methods from the class below?

export default class Cookies {
    static get (name) {...}
    static set (...) {...}
    static remove (...) {...}
}

And is it then possible to import them like this, so people don't have to import the remove method if they don't need it?

import { get, set } from "Cookies"

I am creating a node package to handle cookies. What is the best way to export static class methods from the class below?

export default class Cookies {
    static get (name) {...}
    static set (...) {...}
    static remove (...) {...}
}

And is it then possible to import them like this, so people don't have to import the remove method if they don't need it?

import { get, set } from "Cookies"

Share Improve this question asked Sep 23, 2016 at 13:59 Stefan VerweijStefan Verweij 551 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Since they are static methods, they are basically just properties on the class object. Since that is the case, you can just export them one by one:

export default class Cookies {
    static get (name) {...}
    static set (...) {...}
    static remove (...) {...}
}

export const get = Cookies.get;
export const set = Cookies.set;
export const remove = Cookies.remove;
发布评论

评论列表(0)

  1. 暂无评论