简体   繁体   中英

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?

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:

  • event.target - the reference to clicked element. IE uses event.srcElement instead.
  • event.clientX / event.clientY - coordinates of the pointer at the moment of click.

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

Example:

Like if in HTML you have assigned an event like this

<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.)

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.

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.


Note that there is no such method Event.stopPropagation() . However, the event object will have a stopPropagation() , so the capital E was probably a typo. It's likely that the intent was to use 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 . It's not reserved (although Event is, which is why you can use Event.add ), and you can name it whatever you like.

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.


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 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 });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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