Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
摘自“JavaScript:权威指南,第 4 版”第 7.1 节:
请注意,这些参数变量仅在函数执行时定义;一旦函数返回,它们就不会持续存在。
这是真的吗?这是否意味着如果我打算在嵌套函数中使用它们,我必须将一些参数保存到局部变量中?
您可以像使用任何其他局部变量一样关闭参数,如下所示:
function test(v1) { return function() { alert(v1); } } var f = test("hello"); f();
这只是因为返回的函数在其词法范围内关闭了变量。在正常情况下,是的,参数确实是函数的本地参数,并且一旦函数返回就不会持续存在。