eval javascript, check for syntax error

后端 未结 6 496
失恋的感觉
失恋的感觉 2020-12-08 09:50

I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc... so lets say I use eval for some arbitrary

6条回答
  •  独厮守ぢ
    2020-12-08 10:21

    To continue using the code after validation, I use the following example:

    var validCode = 1;
    try {
      eval( jsCode );        /* Code test */
    } catch (e) {
      if (e instanceof SyntaxError) {
        validCode = 0;
        console.warn(e.message);
      }
    } finally {
      if(validCode){
        "do some magic"
      }
    }
    

提交回复
热议问题