简体   繁体   English

有关document.addEventListener('mousemove',function(e){

[英]Newbie question about document.addEventListener('mousemove', function (e) {

Okay: 好的:

document.addEventListener('mousemove', function (e) {...code...}, false);

Recently I realized that I could greatly enhance my interaction with a few websites by way of Chrome extensions to reorder and rewrite the website to suit my needs. 最近,我意识到我可以通过Chrome扩展程序来重新排序和重写网站以满足我的需要,从而大大增强与几个网站的互动。

So, I've been trying to get a grasp of chrome extensions, javascript, css, dom, jquery and HTML. 因此,我一直试图了解chrome扩展,javascript,css,dom,jquery和HTML。 It is a huge subject and I am woefully unfamiliar with web technologies. 这是一个巨大的主题,我非常不熟悉网络技术。

Can someone please explain what 'function(e){...code...}' is in this context? 有人可以解释一下这种情况下的“功能(e){...代码...}”是什么吗?

It is an inline function without a name? 它是没有名称的内联函数? So, unlike other languages, instead of creating a function with a name and then calling it when needed, this statement hooks the mousemove with an unnamed function? 因此,与其他语言不同,此语句不是用名称创建函数然后在需要时调用它,而是将鼠标移动与未命名的函数挂钩?

I suppose it is a stupid question to ask what the benefit is to having an inlined unnamed function is? 我想问一个内联的未命名函数有什么好处,这是一个愚蠢的问题?

function (e) {...code...} is a reference to an anonymous function to run on the occurence of the mousemove event. function (e) {...code...}是对在mousemove事件发生时运行的匿名函数的引用。 The e parameter is the event Object that is sent with the event itself. e参数是与事件本身一起发送的事件对象。

So basically you say: everytime someone moves his/her mouse around somewhere in the DOM Object document , execute the function using the event Object I give you in the parameter of that function. 因此,基本上,您说的是:每当有人将鼠标移动到DOM Object document中的某个位置时,请使用我在该函数的参数中给您的事件Object执行该函数。

You could've also used (and this is sometimes advised for readability and clarity): 您还可以使用(有时出于可读性和清晰度的考虑,建议您这样做):

function mousemover(e){ ... }
document.addEventListener('mousemove', mousemover, false);

That is also the preferred way if you later on decide to remove the eventlistener ( removeEventListener ). 如果您以后决定删除eventlistener( removeEventListener ),这也是首选方法。

An inline anonymous function is sometimes called a lambda function . 内联匿名函数有时称为lambda函数 You can read about it in this SO Question . 您可以在此SO Question中阅读有关它的信息。

As per request in the comments: in javascript functions are first class objects . 根据注释中的请求:javascript函数中是一流的对象 Specifically, this means that the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures (quoted from this wikipedia page ). 具体来说,这意味着该语言支持将函数作为参数传递给其他函数,将它们作为其他函数的值返回,并将它们分配给变量或将其存储在数据结构中(引用此Wikipedia页面 )。 Also read more on Douglas Crockfords page . 也可以在Douglas Crockfords页面上阅读更多内容。

They are called anonymous functions. 它们称为匿名函数。

You can read a little more about them here: 您可以在这里阅读有关它们的更多信息:

http://en.wikipedia.org/wiki/Anonymous_function#JavaScript http://en.wikipedia.org/wiki/Anonymous_function#JavaScript

Inlined (anonymous) functions are just a style thing, allowing for shorter code. 内联(匿名)函数只是一种样式,允许使用较短的代码。 They can also avoid polluting the namespace by introducing unneeded names into the current scope. 通过将不需要的名称引入当前范围,他们还可以避免污染名称空间。

However in this particular case there is a downside in that it's impossible to remove a specific event listener if it was added anonymously. 但是,在这种特定情况下,存在一个缺点,即如果匿名添加了特定的事件侦听器,则无法删除它。

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

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