这可能是 JS 101 但是...
对 JS 引擎有更好了解的人可以向我解释为什么字符串文字、整数等会被 JS “忽略”或视为其他有效代码吗?
JS 提示确实给出了“意外表达式”报告,但代码仍然有效并运行。
我创建了以下笔,希望能够解释我在这里的意思。
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
最佳答案
造成您困惑的一大罪魁祸首是 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 语法中的许多内容,包括这种对表达式的特殊处理。使用字符串作为语句对其值没有任何作用(除非字符串是“use strict” - 那么它是一个有用的技巧,可以在新浏览器中执行某些操作,但在旧浏览器中执行任何操作)。使用函数调用作为语句运行它会产生副作用并忽略其返回值。如果你忽略返回值,一些更小气的语言,比如 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
It appears that anythign not explicitly wrapped in an eval function is ignored by JS rather than throwing any errors.
感谢上帝,就是这样!如果事情像这样隐式评估,那么字符串永远不应该作为代码运行,除非你明确表示它!
关于javascript - javascript 是否使用某种隐式 "noop"调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163379/