Calling JavaScript function for a top Level Object by using a String

JavaScript Code

var myPanel = null;              /* Our object */
function testFunctions(func) {
    logStatus("Function " + func + " Active !");
    myPanel = createPanel();
    var info = "Calling ANONYMOUS Function" ;
    myPanel.testfunc(info);
    info = "Calling NAMED     Function" ;
    myPanel.testfunc2(info);
    info = "Calling ANONYMOUS Function VIA String" ;
    myPanel.invokeFuncByString(myPanel, "testfunc",info);
    info = "Calling NAMED     Function VIA String" ;
    myPanel.invokeFuncByString(myPanel, "testfunc2",info);
}

function createPanel(vSingleClickInProgess, vClickTimeout) {
    var panel = Object.create(Object.prototype, {
        objectName: {value: "panel", writable: true}      /* just provide a name for our object */
    });
    panel.testfunc  = function (info) {
        logStatus ("__ " + info);
    };
    panel.testfunc2  = function (info) {
        logStatus ("__ " + info);
    };
    panel.invokeFuncByString = function(obj,funcName,info) {
        var fnString= funcName;
        var fn = obj[fnString];
                // is object a function?
        if (typeof fn === "function")
            fn(info + " - ObjectName: " +  obj.objectName  + " - functionName: " + funcName);
        else {
            logErrorT(logHeader, "__ fn IS NOT A Function - : fnString " + fnString + " type: " + typeof fn )
        }
    }
    return panel;
};

Output

17:05:12:740 STATUS::__ Calling ANONYMOUS Function
17:05:12:740 STATUS::__ Calling NAMED     Function
17:05:12:741 STATUS::__ Calling ANONYMOUS Function VIA String - ObjectName: panel - functionName: testfunc
17:05:12:742 STATUS::__ Calling NAMED     Function VIA String - ObjectName: panel - functionName: testfunc2

References

Leave a Reply

Your email address will not be published. Required fields are marked *