简体   繁体   English

ES6:如何在这个类中存储对象?

[英]ES6: How to store the object inside this class?

The TopPage_R class is in certain degree the analogue of R class in the Android SDK (if don't go into details, the R class stores the references to images, string values etc). TopPage_R类在某种程度上类似于 Android SDK 中的R 类(如果不详述, R类存储对图像、字符串值等的引用)。 In my inherited ES6-class, CSS_CLASSE_SELECTORS is the static property (actually the method) that returns the css class selector (eg .header ) by class name (eg header ).在我继承的 ES6 类中, CSS_CLASSE_SELECTORS是静态属性(实际上是方法),它通过类名(例如header )返回 css 类选择器(例如.header )。

class R {

    static getSelectorsOfCssClasses(CssClassesObj){

        let OutputCssClassesObj = CssClassesObj;

        for (let key in OutputCssClassesObj){
            OutputCssClassesObj[key] = '.' + CssClassesObj[key];
        }

        return OutputCssClassesObj;
    }
}

class TopPage_R extends R {

    /* required even if constructor in the superclass nas not beed defined */
    constructor() {
        super();
    } 

    static get CSS_CLASSES () { return {
        header: 'header',
        logo: 'logo',
        navmenu: 'navmenu',
        footer: 'footer'
    }}

    static get CSS_CLASS_SELECTORS(){ 
        return R.getSelectorsOfCssClasses(TopPage_R.CSS_CLASSES)}
    }
}

Every time we call CSS_CLASS_SELECTORS method, new object will be created and the looping will be executed.每次我们调用CSS_CLASS_SELECTORS方法时,都会创建新对象并执行循环。 What the waste of system resources, in't it?浪费什么系统资源,不是吗?

console.log(TopPage_R.CSS_CLASS_SELECTORS.header);
// The new OutputCssClassesObj will be created again
console.log(TopPage_R.CSS_CLASS_SELECTORS.footer);

I need the CSS_CLASS_SELECTORS stored inside the inherited class.我需要存储在继承类中的CSS_CLASS_SELECTORS However I don't want to create the instance of the TopPage_R : I want to call this class and then it's static property as in Android SDK like setToolbarTitle(getString(R.string.app_name));但是我不想创建TopPage_R的实例:我想调用这个类,然后它是 Android SDK 中的静态属性,如setToolbarTitle(getString(R.string.app_name));

let pageResources = new TopPage_R(); // I don't want to create the instance >_<
let $header = jQuery(pageResources.CSS_CLASS_SELECTORS.footer);

Can I store the CSS_CLASS_SELECTORS inside the TopPage_R without creating of it's instance?我可以存储CSS_CLASS_SELECTORS里面TopPage_R没有它的实例的创建? Assume that CSS_CLASS property does not change during web app session.假设CSS_CLASS属性在 Web 应用程序会话期间未更改。

Currently, ES6 does not support static properties, only static methods.目前,ES6 不支持静态属性,只支持静态方法。 But you can use old syntax to set static properties on a class:但是您可以使用旧语法在类上设置静态属性:

class TopPage_R extends R {

    ...

    static get CSS_CLASS_SELECTORS() {
        return R.getSelectorsOfCssClasses(TopPage_R.CSS_CLASSES);
    }
}

TopPage_R.CSS_CLASSES = {
    header: 'header',
    logo: 'logo',
    navmenu: 'navmenu',
    footer: 'footer'
};

Another option, will be to define your css classes as a variable outside of the class.另一种选择是将您的 css 类定义为类之外的变量。 If you are using ES6 modules, then this variable will be hidden from the rest of your app, so no worries about privacy.如果你使用 ES6 模块,那么这个变量将对你的应用程序的其余部分隐藏,所以不用担心隐私。

let CSS_CLASSES = {
    header: 'header',
    logo: 'logo',
    navmenu: 'navmenu',
    footer: 'footer'
};

class TopPage_R extends R {

    static get CSS_CLASSES () {
        return CSS_CLASSES;
    }

    static get CSS_CLASS_SELECTORS() {
        return R.getSelectorsOfCssClasses(TopPage_R.CSS_CLASSES);
    }
}

Or you can use Babel with Class properties transform to compile you code and then use:或者您可以使用带有类属性转换的Babel 来编译您的代码,然后使用:

class TopPage_R extends R {

    static CSS_CLASSES = {
        header: 'header',
        logo: 'logo',
        navmenu: 'navmenu',
        footer: 'footer'
    };

    static get CSS_CLASS_SELECTORS() {
        return R.getSelectorsOfCssClasses(TopPage_R.CSS_CLASSES);
    }
}

I don't know how this works with it being static specifically, but if you want to make a method available to a Class or Object without it running on every new instance you need to add it to the prototype.我不知道它是如何工作的,具体来说是静态的,但是如果您想让一个方法可用于类或对象而不在每个新实例上运行,您需要将其添加到原型中。

R.prototype.getSelectorsOfCssClasses = function(CssClassesObj) {
  ...
}

See this question for more explanation: https://stackoverflow.com/a/5837860/1763258有关更多解释,请参阅此问题: https : //stackoverflow.com/a/5837860/1763258

Consider a classroom full of students.考虑一个满是学生的教室。 Putting something on the prototype is like putting something on the white board for them all to see.把东西放在原型上就像把东西放在白板上给他们所有人看。

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

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