gpt4 book ai didi

javascript - javascript 是否使用某种隐式 "noop"调用?

转载 作者:行者123 更新时间:2023-11-29 17:05:11 25 4
gpt4 key购买 nike

这可能是 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

最佳答案

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 语法中吸取了很多东西,包括这种对表达式的特殊处理。使用字符串作为语句对其值没有任何作用(除非该字符串是“严格使用”——那么它是一个有用的技巧,可以在新浏览器中执行某些操作,但在旧浏览器中不执行任何操作)。使用函数调用作为语句运行它是为了产生副作用并忽略它的返回值。如果您忽略返回值,一些更吝啬的语言,如 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com