简体   繁体   English

js中'function(event)'是什么意思

[英]What is the meaning of 'function(event)' in js

I don't know the meaning of the sentence 'function(event)'我不知道“函数(事件)”这句话的意思

Event.add(apple,'click',function(event) {
    Event.stopPropagation(event);
});

Isn't the argument 'event' is the unique keyword of javascript?参数'event'不是javascript的唯一关键字吗?

Is keyword can be an argument of some function?关键字可以是某个函数的参数吗?

I understand the meaning of below code :我理解以下代码的含义:

function(test) {
alert(test);
}

But I don't understand this one :但我不明白这一点:

function(event)...功能(事件)...

Can any one give an explanation about that to me?任何人都可以向我解释一下吗?

The event object is always passed to the handler and contains a lot of useful information what has happened.事件对象总是传递给处理程序并包含许多有用的信息。

Different types of events provide different properties.不同类型的事件提供不同的属性。 For example, the onclick event object contains:例如,onclick 事件对象包含:

  • event.target - the reference to clicked element. event.target - 对被点击元素的引用。 IE uses event.srcElement instead. IE 使用 event.srcElement 代替。
  • event.clientX / event.clientY - coordinates of the pointer at the moment of click. event.clientX / event.clientY - 单击时指针的坐标。

Information about which button was clicked and other properties.有关单击了哪个按钮和其他属性的信息。 Please visit this link.请访问此链接。
It answers all your questions very simply它非常简单地回答了您的所有问题

Source http://javascript.info/tutorial/obtaining-event-object来源http://javascript.info/tutorial/obtaining-event-object

Example:例子:

Like if in HTML you have assigned an event like this就像在 HTML 中您分配了这样的事件

<button onclick="alert(event)">See the event</button>

then然后

function alert(event) {
    // event.type contains whether this event was invoked in the result of a click etc
    // event.target would contain the reference to the element which invoked this method/event
}

It is an anonymous function, that is a function without name, that sends the event object.它是一个匿名函数,即一个没有名字的函数,它发送事件对象。 That object contains information about the event itself.该对象包含有关事件本身的信息。 It is always passed as first object/variable.它总是作为第一个对象/变量传递。

It is defining an anonymous function object.它定义了一个匿名函数对象。 This code:这段代码:

function foo(bar) { ... }

Is functionally similar to:在功能上类似于:

var foo = function (bar) { ... };

(Except that in the first case the name foo and the creation and assignment of the function object are hoisted to the top of the scope, while in the second case only the name foo is hoisted; foo won't hold the function until the assignment executes.) (除了在第一种情况下,名称foo以及函数对象的创建和赋值被提升到作用域的顶部,而在第二种情况下,只有名称foo被提升;直到赋值时foo才会持有该函数执行。)

Effectively, the code you posted is calling Event.add() and passing a function to it as the third argument, but rather than declaring the function ahead of time it is creating the function object inline.实际上,您发布的代码正在调用Event.add()并将一个函数作为第三个参数传递给它,而不是提前声明该函数,而是创建了内联函数对象。

Another way to write the code block in your question is:在您的问题中编写代码块的另一种方法是:

function handler(event) {
    Event.stopPropagation(event);
}

Event.add(apple, 'click', handler);

Except that the code in your question does not introduce the handler name.除了您问题中的代码没有引入handler名称。


Note that there is no such method Event.stopPropagation() .请注意,没有这样的方法Event.stopPropagation() However, the event object will have a stopPropagation() , so the capital E was probably a typo.然而, event对象会有一个stopPropagation() ,所以大写的 E 可能是一个错字。 It's likely that the intent was to use function (event) { event.stopPropagation(); }很可能意图是使用function (event) { event.stopPropagation(); } function (event) { event.stopPropagation(); } . function (event) { event.stopPropagation(); } .

event is just a variable that's passed to event listener functions such as Event.add , element.on . event只是传递给事件侦听器函数的变量,例如Event.addelement.on It's not reserved (although Event is, which is why you can use Event.add ), and you can name it whatever you like.它不是保留的(尽管Event是,这就是您可以使用Event.add ),并且您可以随意命名它。

The event argument is used to pass information about the event that has happened (the click on apple in this case), which can be used to retrieve data about the event or manipulate it. event参数用于传递有关已发生事件的信息(在本例中为点击apple ),可用于检索有关该事件的数据或对其进行操作。


function(){...} is an anonymous function, which means that you don't need to name it, you can just declare it inline, and the function will be passed as an argument, as if you said function(){...}是一个匿名函数,这意味着你不需要命名它,你可以直接声明它内联,并且该函数将作为参数传递,就像你说的

function foo (event) {
    ...
}
Event.add(apple, "click", foo);

but you don't need to declare it before hand.但你不需要事先声明它。 It does come at the disadvantage of not being duplicable, for instance when clearing an event handler.它确实具有不可复制的缺点,例如在清除事件处理程序时。

Look at the event variable and you will all understand :)看看事件变量你就明白了:)

function (event) {
  console.log({ event });
}

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

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