JSLint “eval is evil.” alternatives

后端 未结 7 1374
别那么骄傲
别那么骄傲 2021-02-13 22:49

I am have some JavaScript functions that run on both the client (browser) and the server (within a Java Rhino context). These are small functions - basically little validators

7条回答
  •  眼角桃花
    2021-02-13 23:26

    Well, the first thing to bear in mind is that jsLint does make the point that "it will hurt your feelings". It's designed to point out where you're not following best practices -- but code that isn't perfect can still work just fine; there's no compulsion upon you to follow jsLint's advice.

    Having said that, eval is evil, and in virtually all cases there is always a way around using it.

    In this case, you could use a library such as require.js, yepnope.js or some other library that is designed to load a script separately. This would allow you to include the javascript functions you need dynamically but without having to eval() them.

    There are probably several other solutions as well, but that was the first one that came to my mind.

    Hope that helps.

提交回复
热议问题