0

I have created custom function with function name as $.

function $(){
    alert("Test");
}

jQuery(function(){
    alert($);
});

When I run this, I get the function body in the alert. But, when I debug this code, on keeping the break-point at alert($) in the browser(Chrome), I also get alert("Test") before the function body.

The same behavior is reproducible on the JSFiddle.

Can someone explain this behavior?

enter image description here

Aditya Singh
  • 9,512
  • 5
  • 32
  • 55

1 Answers1

5

You have a function named $. When executing alert($) you are effectively telling browser to show function $ in the alert window, which it does. $ is not specific here, same will work with abc function:

function abc(){
    alert("Test");
}
jQuery(function(){
    alert(abc);
});

If done like this:

function abc(){
    alert("Test");
}
jQuery(function(){
    alert(abc());
});

It will first run abc() and display "Test", then it will display the result of abc(), which is undefined.

mishik
  • 9,973
  • 9
  • 45
  • 67