【问题标题】:How to avoid this case of evil eval?如何避免这种邪恶评估的情况?
【发布时间】:2012-05-01 02:19:50
【问题描述】:

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

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

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;
}

【问题讨论】:

  • var m = object[property]() 也许?

标签: javascript eval jslint jshint


【解决方案1】:

使用下面的,会好很多,其他地方不用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;
}

【讨论】:

  • 代替(例如)GetNumOfParameter(t) == 0,您可以简单地使用!GetNumOfParameter(t)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多