gpt4 book ai didi

jquery - 新定义函数中参数的命名空间

转载 作者:行者123 更新时间:2023-12-04 16:57:27 31 4
gpt4 key购买 nike

当我在一个函数中定义一个函数并且两者共享相同的变量名称时会发生什么?例如。:

$(document).ready(

$("a").on("click", function( event )
{
// function code goes here
function newfunction( event )
{
// function code dependant on parameter "event" goes here.
// Is "event" now a new variable totally independant to the first
// parameter "event". Were do I find something concerning the
// topic in the jQuery documentary?
}
}

);

);

第 6 行中的第二个“事件”是否完全独立于第 3 行中的第一个“事件”?我在纪录片中找不到任何命名空间

http://learn.jquery.com/about-jquery/

jQuery的。我该去哪里看?

最佳答案

根据我的评论:它们是不同的变量。是的。这是一个 javaScript 功能,而不是 jQuery。尝试搜索“javascript 参数范围”或类似内容。

忽略其他细节,您所拥有的只是嵌套了 2 个函数。参数变量的范围仅在该函数的大括号内。

function a(p1){
// Can only see p1 here
function b(p2){
// can see p2 and p1 here
}
}

如果名称与外部变量的名称匹配,则与代码最接近的范围获胜
function a(p){
// Can only see first p here
function b(p){
// can see second p here as the first is now hidden by the new definition
}
}

关于jquery - 新定义函数中参数的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41043964/

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