I just see the following code here:
function wheel(e) {
preventDefault(e);
}
Beforehand I was used to using like this:
function wheel(e) {
e.preventDefault();
}
So, what's the different between these?
I just see the following code here:
function wheel(e) {
preventDefault(e);
}
Beforehand I was used to using like this:
function wheel(e) {
e.preventDefault();
}
So, what's the different between these?
The difference is that the first one is a syntax error unless you have a function defined with the name preventDefault()
that does something like
function preventDefault(e) {
e.preventDefault();
}
which makes no sense at all.
event.preventDefault
is a native function that exists on the event object
In the link to the post you included, there is an additional function:
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
This function is a user defined function which is a blocking code that only tries to fire the native function if it exists on the object passed in. This is presumably designed so that you can attempt to use the function on a variety of anonymous objects without having to check to see their type first. It also clears any returnValue
which may have existed on the object, which the native function does not do.