Javascript 尝试Catch函数不返回值

Javascript 尝试Catch函数不返回值,javascript,Javascript,我正在做一个项目,我们有一个自定义的Try-Catch函数,我们正在将所有函数包装在其中。Try-Catch函数如下所示 function TryCatch(fxn) { try { //this is the function code that should be called fxn(); } catch (err) { //an error occurred. attempt to log it back to the se

我正在做一个项目,我们有一个自定义的Try-Catch函数,我们正在将所有函数包装在其中。Try-Catch函数如下所示

function TryCatch(fxn) {
    try {
        //this is the function code that should be called
        fxn();
    } catch (err) {
        //an error occurred. attempt to log it back to the server
        TryLogError("", err, false);
    }
}
我们这样使用它

function NewAdministratorDataToSubmit() {
    TryCatch(function () {
        var admin = {
            administratorName: $("#txtNewAdmin").val(),
            administratorPassword: $("#txtPassword").val(),
            administratorEmail: $("#txtAdministratorEmail").val(),
            administratorRoleID: $("#AdminRoles").val(),
            LoginUserName: $("#txtNewAdminUserName").val()
        };
        return admin;
    });
}
然而,由于我们像这样包装它,所以我们没有得到返回的值

我试过这样做

function NewAdministratorDataToSubmit() {
    return {
            TryCatch(function () {
                var admin = {
                    administratorName: $("#txtNewAdmin").val(),
                    administratorPassword: $("#txtPassword").val(),
                    administratorEmail: $("#txtAdministratorEmail").val(),
                    administratorRoleID: $("#AdminRoles").val(),
                    LoginUserName: $("#txtNewAdminUserName").val()
                };
                return admin;
            });
        }
}
这会引发错误,例如 在传递的函数处

对标识符使用关键字无效

以及

TryCatch是语法错误

因此,我需要修复TryCatch函数以返回值,或者向只返回值的函数添加一个返回值。但并非所有函数都返回值

TryCatch用于所有功能


有什么想法吗?

您缺少一条返回语句

function TryCatch(fxn) {
    try {
        return fxn(); // return the result of the function call
    } catch (err) {
        TryLogError("", err, false);
    }
}

如果传递的函数不返回任何值,会发生什么情况?@Chris函数总是返回一个值。如果没有
return
语句,它将返回
undefined
。然后您将得到
undefined
,javascript函数调用的默认值。@Chris缺少的return语句不是来自
fxn()
函数。它是返回执行
fxn()
的结果的return语句。以这种方式包装每一段代码似乎是一个真正的PitA,为什么不直接使用
window.onerror()