简体   繁体   English

如何在 JS 类中定义静态变量

[英]How to define a static variable inside a JS class

I'm building a static class to store data inside an a array and i want to declare a static variable but i don't know how to do it on javascript.我正在构建一个静态类来将数据存储在一个数组中,我想声明一个静态变量,但我不知道如何在 javascript 上执行此操作。

JS code: JS代码:

class GCache {

    // Define cache variable
    static cache = {}; // <--- that is what i don't know how to do

    /**
     * Check if data is on cache
     * @param {*} id 
     */
    static isOnCache(id){
        return this.cache.hasOwnProperty(id);
    }

    /**
     * Add data to cache
     * @param {*} id 
     * @param {*} json 
     */
    static addToCache(id, json){
        if(this.isOnCache(id)) return;
        this.cache[id] = json;
    }

    /**
     * Obtain data from cache
     * @param {*} id 
     */
    static getFromCache(id){
        if(this.isOnCache(id)) return;
        return this.cache[id];
    }

}

Thank you <3谢谢<3

Currently* (see bottom of answer for details), non-function properties can't be added to a class itself inside the class definition.目前*(有关详细信息,请参见答案底部),无法将非函数属性添加到类定义中的类本身。 It looks ugly, but you'll have to assign the property outside of the class definition:它看起来很难看,但您必须在类定义之外分配属性:

class GCache {
  ...
}
GCache.cache = {};

Also note that your getFromCache function probably has a bug: you probably want to return early if the id being searched for does not exist in the cache:另请注意,您的getFromCache函数可能有一个错误:如果正在搜索的id存在于缓存中,您可能希望提前返回:

if(!this.isOnCache(id)) return;

 class GCache { /** * Check if data is on cache * @param {*} id */ static isOnCache(id){ return this.cache.hasOwnProperty(id); } /** * Add data to cache * @param {*} id * @param {*} json */ static addToCache(id, json){ if(this.isOnCache(id)) return; this.cache[id] = json; } /** * Obtain data from cache * @param {*} id */ static getFromCache(id){ if(!this.isOnCache(id)) return; return this.cache[id]; } } GCache.cache = {}; GCache.addToCache('myid', 'data'); console.log(GCache.getFromCache('myid'));

But, in this case, it would probably be easier to use a plain object, rather than a class.但是,在这种情况下,使用普通对象可能比使用类更容易。 The class isn't being used to instantiate anything, after all, and with an object, you can both define cache inside the object , and reduce the syntax noise by getting rid of all the static s:毕竟,该类并未用于实例化任何内容,并且对于一个对象,您既可以在 object 内部定义cache ,又可以通过摆脱所有static减少语法噪音:

 const GCache = { cache: {}, isOnCache(id) { return this.cache.hasOwnProperty(id); }, addToCache(id, json) { if (this.isOnCache(id)) return; this.cache[id] = json; }, getFromCache(id) { if (!this.isOnCache(id)) return; return this.cache[id]; } } GCache.addToCache('myid', 'data'); console.log(GCache.getFromCache('myid'));


There is currently a proposal allowing you to set static non-method properties onto a class.当前有一个提议允许您将静态非方法属性设置到类上。 It's currently at Stage 2, which means it's expected to eventually be implemented officially.它目前处于第 2 阶段,这意味着它有望最终正式实施。 Once it lands, the code:一旦着陆,代码:

class GCache {
  ...
}
GCache.cache = {};

can be replaced by:可以替换为:

class GCache {
  static cache = {};
  ...
}

如果您的意思是类的所有实例的静态变量,请在您的类声明下方声明: GCache.cache = {}

Babel's plugin-proposal-class-properties is probably what you're looking for. Babel 的plugin-proposal-class-properties可能就是你要找的。 It enables support for static variables/methods inside the class declaration.它支持类声明中的静态变量/方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM