Javascript错误:SyntaxError:eval()函数中的语法错误

问题描述 投票:-1回答:1

我已经在下面的这条字符串中存储了一个变量javascript_string_above

function run(hideBtn, list) {
  if(hideBtn) {
    return list.filter(function(x) { return x.period == 1; });
  }
  else {
    return list;
  }
}

run(hideBtn, list);

上面的字符串像下面那样传递给eval函数:

const hideBtn = true;
const list = this.list; //assuming this list has values in it.
const resultList = eval(javascript_string_above);

评估之后,我在控制台日志中收到语法错误。这是错误:

Error: Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)
   {
      [functions]: ,
      __proto__: { },
      description: "Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)",
      message: "Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)",
      name: "Error",
      promise: { },
      rejection: { },
      stack: "Error: Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)
   at resolvePromise (eval code:824:25)
   at ZoneAwarePromise (eval code:893:17)
   at Anonymous function (eval code:38:5)
   at Anonymous function (eval code:176:43)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval ",
      Symbol(observable)_h.143sf9as4rn: undefined,
      Symbol(rxSubscriber)_g.143sf9as4rn: undefined,
      task: { },
      zone: { }
   }

[请帮忙,我这个问题已经存在很长时间了。 仅在IE11中遇到

javascript internet-explorer-11 eval
1个回答
0
投票

我找到了根本原因。 filter()方法内部的函数被转换为lambada表达式:

FROM

list.filter(function(x) { return x.period == 1; });

TO

list.filter(x => x.period == 1);

IE11不支持箭头功能“ =>”,因此引发语法错误。

© www.soinside.com 2019 - 2024. All rights reserved.