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?
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?
Share Improve this question asked Aug 31, 2014 at 17:20 goJsongoJson 2332 gold badges6 silver badges13 bronze badges 2- 1 As you can see here: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… event is not a reserved word in javascript grammar. – pfooti Commented Aug 31, 2014 at 17:27
-
In this case, function sent into
Event.add
as its third argument is so-called event handler - it will be called when the corresponding event (click
here) occurs, and the event object itself will be used as its first param. From purely technical point-of-view, it doesn't matter what name is used. But for readability reasons, usingevent
here matters much. – raina77ow Commented Aug 31, 2014 at 17:28
5 Answers
Reset to default 6The 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(); }
.
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 e 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 });
}