最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to eliminate error: "Implied eval is evil" - Stack Overflow

programmeradmin1浏览0评论

I'm trying to make my code JavaScript "strict", so I'm running it through JSLint to ensure my code is compliant.

However, on the following code:

setTimeout("getExtJs()", 2000);

I receive the following error:

Implied eval is evil. Pass a function instead of a string.

How do I make my code JavaScript "strict"?

I'm trying to make my code JavaScript "strict", so I'm running it through JSLint to ensure my code is compliant.

However, on the following code:

setTimeout("getExtJs()", 2000);

I receive the following error:

Implied eval is evil. Pass a function instead of a string.

How do I make my code JavaScript "strict"?

Share Improve this question edited Apr 13, 2019 at 11:25 showdev 29.2k37 gold badges59 silver badges79 bronze badges asked Nov 12, 2010 at 18:07 HeatherKHeatherK 2,3234 gold badges20 silver badges12 bronze badges 1
  • See Implied eval is evil. – showdev Commented Apr 13, 2019 at 11:22
Add a comment  | 

5 Answers 5

Reset to default 14
setTimeout(getExtJs, 2000);

Note that there are no quotes around getExtJs, I am passing the function not a String.

EDIT: As noted in the comments the reason why JSLint is upset is that when the first argument is a String it is processed as code to be executed in the same manner as eval()

See https://developer.mozilla.org/en/window.setTimeout

To find out why eval() (and by extension using Strings as the 1st argument here) is evil see the Mozilla Developer Network entry for eval.

It shouldn't complain if you do:

setTimeout(function(){
  // your code of this function getExtJs here
}, 2000);

Or:

setTimeout(getExtJs, 2000);

Although I don't see anything wrong in your implementation security-wise or otherwise.

Like it says, pass in the function (no quotes around the function name):

setTimeout(getExtJs, 2000);

When you pass in a string ("getExtJs"), setTimeout end up evaling it. Instead of that, it is better to simply pass in the function itself (getExtJs).

setTimeout(function () {getExtJs();}, 2000);

The correct syntax is

setTimeout(getExtJs, 2000);

You pass a reference to the function, and after 2000ms the function is executed. If you put parens after the function name, you are executing the function instead of referencing it.

发布评论

评论列表(0)

  1. 暂无评论