我有一个代码,该代码有望返回"未定义"或任何错误,但它给出了如上所述的输出。
let i = (x, c) => {
c(x);
};
i(20, (undefined) => {
let j = undefined;
console.log(j);
});
function y(undefined) {
let a = undefined;
console.log(a);
}
y(90);
保留单词如何有效并改变行为,如果在函数参数中使用?
undefined
不是保留的单词。
它是一个全局,可读的变量。
没有什么可以阻止您在较窄范围中使用相同名称定义另一个变量。
undefined
是不是保留的单词,您可以在此处看到:https://developer.mozilla.org/en-en-us/docs/web/javascript/参考/Lexical_grammar#关键字。
在您的方案中,undefined
实际上具有回调值,在第一种情况下为20,在第二种情况下为90。
例如,如果要将值undefined
分配给示波器j
或a
,则可以使用void 0
。在此处找到有关void
如何工作的进一步信息(此外,void
实际上是JavaScript中的一个保留单词(:https://developer.mozilla.org/en-en-us/docs/web/javascript/javascript/reference/reference/reference/reference/operators/operators/voideators/voidepation/P>
let i = (x, c) => {
c(x);
};
i(20, (undefined) => {
let j = void 0;
console.log(j);
});
function y(undefined) {
let a = void 0;
console.log(a);
}
y(90);