简体   繁体   English

用户输入的JavaScript eval与变量声明

[英]User input JavaScript eval with variable declarations

As part of a homework I am trying to implement a JavaScript console similar to the one available in Firebug in a browser environment. 作为家庭作业的一部分,我试图实现一个JavaScript控制台,该控制台类似于浏览器环境中Firebug中可用的控制台。 From what I've picked up, eval() seems to be the easiest way to do this. 根据我的选择, eval()似乎是最简单的方法。 However, my code runs into some problems on even very basic user input. 但是,即使是非常基本的用户输入,我的代码也会遇到一些问题。 For example: 例如:

var number = 5;

Causes a syntax error rather than just evaluating to undefined like it would in Firebug. 导致语法错误,而不仅仅是像Firebug中那样评估为undefined Because of this I can't seem to declare variables at all inside the eval string. 因此,我似乎根本无法在eval字符串中声明变量。 If I do something more simple like: 如果我做一些更简单的事情,例如:

3 + 4 * Math.PI

It works correctly. 它可以正常工作。 I have tried to find an example of someone using eval() on a string containing a variable declaration, and I just can't seem to find anyone doing this. 我试图找到一个在包含变量声明的字符串上使用eval()的示例,但似乎找不到任何这样做的示例。

Do I need to parse the user input completely using regular expressions before compiling it into a new string for eval() ? 我需要使用正则表达式完全解析用户输入,然后再将其编译为eval()的新字符串吗?

Can eval() understand semicolons as line breaks? eval()可以将分号理解为换行符吗? I can't find people using these either. 我也找不到使用它们的人。

function runMiniFirebug() {
    var userInput = document.getElementById("user-input").value;
    try {
        var userOutput = eval('(' + userInput + ')');
        document.getElementById("js-output").innerHTML += '<p class="input">>>>' + userInput + '<p>';
        document.getElementById("js-output").innerHTML += '<p class="ouput">' + userOutput + '<p>';
    }
    catch(error) {
        document.getElementById("js-output").innerHTML += '<p class="input">>>>' + userInput + '<p>';
        document.getElementById("js-output").innerHTML += '<p class="error">' + error.message + '<p>';
    }
}

EDIT: So it seems the added parens are causing the error. 编辑:所以似乎添加的括号引起了错误。 This is a section from my instructors slides. 这是我的讲师幻灯片的一部分。 Is the information incorrect, or am I just interpreting it incorrectly? 信息是否正确,或者我只是错误地解释了信息?

Strings that are delimited with { ... } 用{...}分隔的字符串

– You have to add extra parens so that JavaScript will know that the braces are for object literals, not for delimiting statements. –您必须添加额外的括号,以便JavaScript知道括号用于对象文字,而不是用于分隔语句。

• It never hurts to do this, so add parens routinely •这样做无济于事,因此请定期添加括号

– var test2 = "{ firstName: 'Jay', lastName: 'Sahn' }"; – var test2 =“ {firstname:'Jay',lastName:'Sahn'}”;

– var person = eval("(" + test2 + ")"); – var person = eval(“(” + test2 +“)”);

var userOutput = eval('(' + userInput + ')');

Why are you wrapping with parentheses? 为什么用括号括起来? This creates the statement 这将创建语句

(var number = 5;)

which is invalid syntax. 这是无效的语法。

Simply remove the '(' + and + ')' . 只需删除'(' ++ ')'


As for your edit, that is referring to only evaluating single expressions . 至于您的编辑,那是指仅对单个表达式求值 var number = 5; is not an expression, nor is alert(1 + 1); alert(2 + 2); 不是表达式,也不是alert(1 + 1); alert(2 + 2); alert(1 + 1); alert(2 + 2); . Wrapping either in parentheses will cause an error. 用括号括起来会导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM