简体   繁体   中英

Exporting Javascript Class Functions

I am trying to implement the module YSlow into my own project. A function I want to use looks like this:

YSLOW.registerRuleset = function (ruleset) {
    YSLOW.controller.addRuleset(ruleset);
};

From what I can tell, YSlow is assigned here in the code at the beginning:

if (typeof YSLOW === 'undefined') {
    YSLOW = {};
}

What I want to be able to do is export this class and be able to use this function. I would usually just put:

exports.sayHello = function() {
    return "Hello"
};

However, I am unsure how to properly communicate with registerRuleset() when YSLOW is in the beginning. If I put sayHello() in yslow.js I can use it fine, but for any function with YSLOW at the beginning it does not work. I have tried putting exports before and after YSLOW in a function but have had no success.

So my question is, how can I get around this? And for that matter, what is YSLOW.function() even doing exactly and what is the importance of it?

Thanks!

I'm not familiar with YSLOW, hence I'm not clear with your query.

If you are trying to check whether YSLOW class objects are loaded, below code may help to identify whether loaded or not loaded. this code will keep polling for the objects to be ready and available. This will poll for 20 milliseconds (you can increase to more).

Its a Kind of hack, you can keep polling for the objects whether it is loaded and ready to use.

   function checkLoaded() {
        if (YSLOW && YSLOW.controller && YSLOW.controller.addRuleset) {
            return true;
        }
        return false;
    }

    /* Start: polling to check if YSLOW Objects are ready */
    var timeout = Date.now() + 10000,
        startTime = Date.now();
    var intervalID = window.setInterval(function () {
        if (checkLoaded()) {
            console.log("GOT Ready at: ", Date.now() - startTime + "ms");
            window.clearInterval(intervalID);
            initLoad();//do your stuffs after loading here.
        } else if (Date.now() > timeout) {
            console.log("Not loaded, kill the polling.");
            window.clearInterval(intervalID);
            failedLoad(); //do your stuff for not loaded here
        }
    }, 20);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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