javascript是否使用某种隐含的" noop"呼叫?

时间:2014-08-06 14:43:16

标签: javascript computer-science

这可能是JS 101但......

对JS引擎有更好了解的人能否向我解释为什么JS会忽略字符串文字,整数等等,或者将其视为其他有效代码?

JS Hint确实提供了“意外表达”报告,但代码仍然有效且运行。

我已经创建了以下笔,希望能解释我的意思。

http://codepen.io/billythekid/pen/zyGbi/

var span = document.getElementsByTagName('SPAN')[0];

// let's show what I'm trying to say here by way of an expanded example.
function foo()
{
  var bar = "something";
}
foo(); // does nothing useful, but returns nothing either - valid and understandable;

function baz()
{
  return "nothing"; // a string
}

function zoo()
{
  return 250; // an integer
}

var a = baz(); // the variable holds the return value. The function is evaluated and the return value is assigned to the variable.
span.innerHTML += a+"<br>";

baz(); // produces no error despite the function returning a value. Why doesn't the JS engine see this as the evaluated string "something" and try to run the string as a JS command?
span.innerHTML += "this code has run, so the JS didn't break above. Why wasn't the returned string parsed and invalid?<br>";

"something"; // the string literal
span.innerHTML += "this code has run, so the JS didn't break above. So why not? How is a string literal valid JS? Why no errors?<br>";

var b = zoo();
span.innerHTML += b+"<br>";

zoo();// produces no error despite the function returning a value. Why doesn't the JS engine see this as the evaluated integer 250 and try to run the string as a JS command?
span.innerHTML += "this code has run, so the JS didn't break above. So why not? How is an evaluated integer valid JS? Why no errors?<br>";

250; // the integer literal
span.innerHTML += "this code has run, so the JS didn't break above. So why not? How is an integer literal valid JS? Why no errors?<br>";

eval(250); // the integer literal
span.innerHTML += "this code has run, so the JS didn't break above. So why not? How is an evaluated integer literal valid JS? Why no errors?<br>";

eval("something"); // the string literal
span.innerHTML += "this code broke, it can't run a string that's not been defined as a function or whatever.<br>";

// and, had the previous code not broken...
non_func(); // doesn't exist!
span.innerHTML += "this code doesn't run! So it'll error out with a call to a function/variable that doesn't exist but not eval'd code that isn't JS, such as a string, or even the literals of these objects!<br>";

// It appears that anythign not explicitly wrapped in an eval function is ignored by JS rather than throwing any errors. Why is this?

只需运行字符串文字,例如“foo”;因为控制台中的一条线似乎会自行返回。

JS是否在某种“noop”方法中内部包装这样的简单案例,或者在内部垃圾收集这样的事情,或者它只是看到代码在经过它之后“运行”并且没有更多的事情要做(比如为变量或其他东西赋值?

我在使用setInterval()调用时考虑到这一点,如果我将它的返回值(以及它的ID标识符)分配给var,以便稍后在clearInterval中使用它有效但是当我们忽略返回时它也是有效的ID。该ID不会被“解析”为JS。

使用严格模式似乎对此行为没有任何影响。

希望我没有比这更令人困惑。 :OD

1 个答案:

答案 0 :(得分:5)

混乱背后的一个罪魁祸首是C编程语言。在其中,您认为是语句的许多内容(例如赋值)实际上都是表达式。

//This is valid C and Javascript code
x = (y += 1);

//We all have been bitten by this one once haven't we?
while(x = y){
}

并且为了让这些语句在它们自己的行中使用,语言语法中有一条规则允许表达式后跟分号计算为语句

stmt :=
   <if_then_else> |
   <while loop> |
   ... |
   <expression> ';'

评估这些单表达式语句的规则是评估表达式的副作用,并忽略其值。

Javascript(以及许多其他语言)从C语法中采用了很多东西,包括对表达式的这种特殊处理。使用字符串作为语句不会对其值有任何作用(除非字符串是&#34;使用严格&#34; - 然后它是一个有用的hack,它在新浏览器中执行某些操作但在旧浏览器中没有任何功能)。使用函数调用作为语句运行副作用并忽略其返回值。如果忽略返回值,一些更吝啬的语言,如Haskell或Go会抱怨,但C和Javascript只会抛弃它们。

-- In Haskell you get a compiler warning for ignoring return values
do
   functionThatReturnsNothing()
   a <- functionThatReturnsAValue()
   _ <- functionThatReturnsAValue() -- unless you ignore it explicitly
  

JS似乎忽略了未明确包含在eval函数中的任何符号,而不是抛出任何错误。

以这种方式感谢上帝!如果事情被隐式评估,那将是一个疯狂的错误,并且字符串永远不应该作为代码运行,除非你明确它!