如何避免这种邪恶的评估?

时间:2012-04-19 14:27:32

标签: javascript eval jslint jshint

我继承了javascript代码库,我是javascript的新手。所以我使用JSHint来避免常见的错误,误用。

JSHint找到了这段代码,但我不知道如何避免邪恶的评估:

function GetProperties(object) {
    var result, property, t;
    result = '';
    for (property in object) {
        if (property.indexOf('Get', 0) === 0) {
            t = object[property] + "...";
            eval("if (GetNumOfParameter(t) == 0) var m = object." + property + "(); else var m = -100;");

            if (window.m != -100) {
                result += property + ': ' + window.m + '\r\n';
            }
        }
    }
    return result;
}

1 个答案:

答案 0 :(得分:2)

使用以下内容,它更好,如果您不在其他地方使用它,则无需使用m

function GetProperties(object) {
    var result, property, t;
    result = '';
    for (property in object) {
        if (property.indexOf('Get', 0) === 0) {
            t = object[property] + "...";

            if (GetNumOfParameter(t) == 0) 
                result += property + ': ' + object[property]() + '\r\n';
        }
    }
    return result;
}
相关问题