0

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?

Community
  • 1
  • 1
Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68

2 Answers2

1

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

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

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.

Community
  • 1
  • 1
Claies
  • 22,124
  • 4
  • 53
  • 77