简体   繁体   中英

How to call TypeScript Static Method from window element?

I'm converting some of our application from JavaScript to TypeScript, and have a JavaScript method that execute functions via the window element by the function name.

We are having trouble finding and executing the correct function from the TypeScript produced JavaScript.

TypeScript Code:

class ItemSelector {
public static preProcesssData(data: any): any {
    //Do Stuff
    return data;
}}

Produced JavaScript

var ItemSelector = (function () {
function ItemSelector() {
}
ItemSelector.preProcesssData = function (data) {
    //Do Stuff
    return data;
};
return ItemSelector;}());

JavaScript Calling Function

function callFunctionByName(funcName,data) {
var fn = null;
/*
 * If function has a scope (namespace) like this: "myScope.myFunction"
 */
if (funcName.indexOf(".") != -1) {
    var ns = funcName.split(".");
    if (ns && ns.length == 2) {
        fn = window[ns[0]][ns[1]];
    } else {
        cl("Function with multiple scopes not supported: " + funcName);
    }
}
/*
 * Else function is just a plain string with no scope.
 */
else {
    fn = window[funcName];
}
//fn is always undefined
if (typeof fn === "function") { fn(data);}}

Example Call

callFunctionByName("ItemSelector.preProcessData",{});

window["ItemSelector"]["preProcessData"] returns undefined.

window["Itemselector.preProcessData"] also returns undefined.

I'm sure this is a very easy one, but I just can't figure it out. Thanks, Matt

你拼ProcessProcesss在你的类。

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