Reputation: 90
I know this may be a very basic question, sorry if it is but in javascript/jquery i sometimes come into contact with a function having a random parameter that doesn't seem to serve an obvious purpose, such as this one below used on the jquery website.
$("div").mousemove(function(e){
var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
$("span:first").text("( e.pageX, e.pageY ) : " + pageCoords);
$("span:last").text("( e.clientX, e.clientY ) : " + clientCoords);
});
Here the function has a parameter of e, i don't get why though i know if it didn't have an e it could also work because that's how i tend to use javascript. Why would a programmer do this?
Upvotes: 2
Views: 230
Reputation: 7517
It isn't random, it is always there. But if you don't want to use it, you can just leave it away. That parameter (e in this case) is the event. They use it to get the coordinates of the mousemove, these are stored in the event object and passed to the function. The event is not the only parameter that is passed to this function, as you can see here, although the others are quite rare.
Upvotes: 1
Reputation: 5905
The e
represents the event object. In the example given, the user needs the event object so that he/she can access the location of the cursor on the screen when the mousemove event is fired, using the pageX
and pageY
properties of the event (e) object.
Upvotes: 6