0

If you omit a parameter to a callback function, it still works. For example,

var callback1 = function() {
    $("span").prepend("Callback 1: " +  event.pageX + " " + event.pageY + "<br>");
};
$("div").click(callback1);

works when the event parameter is not passed. It seems equivalent to:

var callback2 = function(event) {
    $("span").prepend("Callback 2: " +  event.pageX + " " + event.pageY + "<br>");
};
$("div").click(callback2);

with the parameter passed as I feel it should be.

Working JSFiddle of this here.


I found this JS phenomenon peculiar. Why does it work?

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • It **only** works when you're referring to `event`, and it really isn't working in your example because `event` refers to the browser's version, not the augmented/normalized jQuery Event instance. – Pointy Feb 29 '16 at 23:17
  • Because Internet Explorer compatibility. It *should* not work… – Bergi Feb 29 '16 at 23:18
  • Oh, and it does not work in Firefox. – Pointy Feb 29 '16 at 23:18
  • @Bergi But it doesn't only happen with `event`. It happens with other variables as well. – Jonathan Lam Feb 29 '16 at 23:19
  • @JonathanLam: That I doubt. Which ones specifically? Of course, there are many global variables that have *some* values; but no - a function doesn't magically make undeclared variables become parameters. – Bergi Feb 29 '16 at 23:20
  • @Bergi I'll post an example. – Jonathan Lam Feb 29 '16 at 23:20
  • Your `print` function has that `data` parameter declared. Make it `function print() { … }` and it will stop working. – Bergi Feb 29 '16 at 23:22
  • @Bergi Oops. I thought it didn't never mind, and thanks for your time. – Jonathan Lam Feb 29 '16 at 23:24

0 Answers0