Javascript参数(Javascript arguments)

我正在学习更多关于JavaScript的方法,并且在大多数时候它可能会变得非常混乱。

所以我们在jQuery中有这个:

$.getJSON('getDate.php', function(response, status, xhr) { ... });

和这个:

$('a').click(function(event) { ... });

根据上面的代码和javascript定义,请考虑下面列出的这些参数( response, status, xhr和event )以及有关它们的问题:

他们究竟是什么? 他们来自哪里? 你什么时候使用它们? 什么是使用它们的正确方式?

我是一个新手程序员,我不确定我是否问了正确的问题。

谢谢。

I'm on my way to learning more about javascript and it could get really confusing at most times.

So we have this in jQuery:

$.getJSON('getDate.php', function(response, status, xhr) { ... });

And this:

$('a').click(function(event) { ... });

Based on the code above and in terms of javascript definitions, please consider these arguments(response, status, xhr & event) and the questions about them as listed below:

What are they exactly? Where do they come from? When do you actually use them? What's the proper way of using them?

I'm a newbie programmer and I'm not sure if I asked the right questions.

Thanks.

最满意答案

它们是在被调用时提供给你的函数的参数。 请注意名称不重要,但排序是。

他们来自调用代码。 在这种情况下,那将是jQuery。 它将为这些参数设置适当的值,并在调用时将它们传递给您的函数。

当你需要/想要。 查看jQuery文档,了解每个参数包含的信息。 这会给你一个更好的想法,你什么时候可能想用给定的参数来做某件事。

只需在你的函数代码中引用它们(即{和}之间的内容)。 它们和JavaScript中的其他变量一样。 请参阅jQuery文档以获取每个不同参数上可用的字段列表。

They are parameters that will be provided to your function at the time it is invoked. Note that the names are not significant, but the ordering is.

They come from the calling code. In this case, that would be jQuery. It will set up the appropriate values for these parameters and pass them to your function(s) at invocation time.

When you need/want to. Check the jQuery documentation for details on exactly what information each parameter contains. That will give you a better idea on when you might want to do something with a given parameter.

Just refer to them by name in your function code (i.e. the stuff between { and }). They work the same as any other variable in JavaScript. Refer to the jQuery documentation for a list of fields that are available on each different parameter.

更多推荐