Isaac Lubow
Isaac Lubow

Reputation: 3573

What makes an argument into an event object?

As noted in this question, in a function

function(e){
    e.stopPropogation();
}

e is the event object. But why? Is the first and only argument assumed to be the event? What if there are two arguments? How would I make the second refer to the event instead? Or is it as simple as saying

function(something,x){
    x.stopPropogation();
}

because I would assume x would be undefined, and wouldn't expect it to automatically become an event object by virtue of having event methods called on it.

Upvotes: 3

Views: 111

Answers (2)

Pointy
Pointy

Reputation: 413717

That question you reference is specifically about jQuery event handlers. The event object is the first argument to the handler because that's just how the function is invoked. Nothing forces it to be the event object.

You generally cannot control how event handlers are invoked, though some frameworks will let you pass through additional parameters at the time the handler is bound (which means that the same values would be passed through for each event occurrence) or at the time an event is programatically triggered.

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

For DOM events, the first argument (and only one usually) is the event object.

For frameworks, it depends on the implementation, but to maintain consistency they will usually pass the event in the first parameter as well..

You are right to assume the x will be undefined..

have a look at https://developer.mozilla.org/en/DOM/event

Upvotes: 3

Related Questions