作为函数参数传递的字符串未定义 [英] String passed as function parameter is undefined
本文介绍了作为函数参数传递的字符串未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将字符串作为参数传递给函数时,它返回 undefined
.为什么会这样?
When I pass a string to a function as a parameter, it returns undefined
. Why is that?
let a = 'b';
let test = (a) => console.log(a);
test(); // undefined
推荐答案
调用 test();
时,您不会在(
和)
,因此您不会传递任何参数.
When you call test();
you aren't putting anything between (
and )
, so you aren't passing any parameters.
在定义 test
(使用(a)=>
)时,您创建了 local 变量 a
会屏蔽具有相同名称的 global 变量.
When you defined test
(with (a) =>
) you created a local variable a
that masks the global variable with the same name.
要通过 a
,您必须实际通过它: test(a)
.
To pass a
you have to actually pass it: test(a)
.
这篇关于作为函数参数传递的字符串未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文