简体   繁体   English

无法解释的参数传递给JavaScript函数,这是我所缺少的基本知识

[英]Unexplained parameters passed into JavaScript functions, something fundamental I am missing

I have been teaching myself JavaScript over the last Month now, not super consistently since my work has been all over the place, when I get downtime my job is to work on extensions for our sales team. 我过去一个月以来一直在自学JavaScript,但自从我的工作已经遍地开花之后,我的工作就变得非常不稳定。当我遇到停机时,我的工作就是为我们的销售团队进行扩展。

Right now I don't have a specific issue that i can't solve, but I have a question that makes me think that there is something very different about functions in javascript that I am still missing. 现在,我没有一个我无法解决的特定问题,但是我有一个问题让我认为,我仍然缺少关于javascript中的函数的一些不同之处。

Look at this code, and I will explain what confuses me about it: 看下面的代码,我将解释什么使我感到困惑:

function click(e) {
  var selection = e.target.id;
}

document.addEventListener('DOMContentLoaded', function () {
  var divs = document.querySelectorAll('div');
  for (var i = 0; i < divs.length; i++) {
    divs[i].addEventListener('click', click);
  }
});

So, in this code, I understand what is going on except how the click(e) part. 因此,在这段代码中,我了解了发生了什么,除了click(e)部分如何。 The 'e' is an event object correct? “ e”是事件对象正确吗? It is not clear to me how that got passed, and how it knows that 'e' means that. 我不清楚这是如何通过的,以及如何知道“ e”的含义。 I assume I could replace the e with "foo" and it would work still, but exactly what is happening is not clear. 我以为我可以将e替换为“ foo”,并且仍然可以使用,但是目前尚不清楚。 I am pretty sure it has to do with this line of code: 我很确定这与以下代码行有关:

divs[i].addEventListener('click', click);

But I don't understand what is happening behind the scenes to make that happen the way it does. 但是我不了解幕后发生的事情以这种方式实现。

Another example is this from the message passing at http://developer.chrome.com/extensions/messaging.html : 另一个示例是来自http://developer.chrome.com/extensions/messaging.html传递的消息:

contentscript.js
================
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});


background.html
===============
chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

'response' in this is not clear to me where it is coming from, much like 'e' in the other example. 我不清楚其中的“响应”来自何处,就像另一个示例中的“ e”一样。 Any help demystifying how this works would be appreciated, I am open to learning, and I haven't found a good explanation about this. 任何有助于揭开其神秘面纱的帮助将不胜感激,我乐于学习,并且我还没有找到很好的解释。

The event object is passed through the function by the browser itself. 事件对象由浏览器本身通过函数传递。

In case there is an event and a respective event handler is attached, the browser calls that event handler and passes an event object with some (more or less) relevant information about the event to the event handler. 如果存在事件并附加了相应的事件处理程序,则浏览器将调用该事件处理程序,并将带有一些(或更少)有关该事件的相关信息的事件对象传递给事件处理程序。

So with respect to your first example: 因此,关于您的第一个示例:

First the function click( e ) is defined in a regular way. 首先,以常规方式定义click( e )函数。

Afterwards two event handlers are registered: 之后,将注册两个事件处理程序:

  1. for the event DOMContentLoaded 对于事件DOMContentLoaded
  2. for a click event on multiple <div> elements. 多个<div>元素上的click事件。

For the first handler an anonymous function is used. 对于第一个处理程序,使用匿名函数。

document.addEventListener('DOMContentLoaded', function () {
  // do stuff here
});

Here the event object is omitted as it is probably not needed. 这里事件对象被省略,因为可能不需要它。

In the second case the <div> elements all get the same event handler, namely click(e) . 在第二种情况下, <div>元素都获得相同的事件处理程序,即click(e)

divs[i].addEventListener('click', click);

Here, however, the event object is captured as a parameter by the function as it is needed inside the function body. 但是,在这里,事件对象被函数捕获为参数,因为函数体内需要它。

In general in JavaScript you don't have to define all parameters either in the function declaration nor in the call of a function. 通常,在JavaScript中,您不必在函数声明中或在函数调用中都定义所有参数。 You just define the parameters needed and they are applied in the order given. 您只需定义所需的参数,然后按给定的顺序应用它们。 That's why in the first event handler's definition the parameter for the event object can be omitted without any errors. 这就是为什么在第一个事件处理程序的定义中可以省略事件对象的参数而不会出现任何错误的原因。

The click function is invoked by the browser in response to a click event. 浏览器响应单击事件而调用click功能。 The browser passes the appropriate event object as the first argument. 浏览器将适当的事件对象作为第一个参数传递。

Also, you're correct that e can be anything. 此外,您是正确的, e可以是任何东西。 You can give the parameter any (legal) name you want. 您可以为参数指定所需的任何(合法)名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM