Reputation: 3697
I've never seen anyone do this before. Could anyone enlighten me on this? This was taken from here.
jQuery(function ($) { //What's with the $ as an argument?
$('#flux').bind('scroll', function () {
if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
alert('end reached');
}
})
});
It appears he can declare a function and run it at the same time.
Upvotes: 2
Views: 58
Reputation: 262860
It appears he can declare a function and run it at the same time.
The function is not run here, it is just passed to jQuery
(which will run it when the document is ready).
function ($) { //What's with the $ as an argument?
This is the Javascript concept of a module:
Instead of having the function refer to a global $
(which may end up being something unexpected), you pass in the $
as a parameter (jQuery will do that for you here).
Then within the function body $
refers to that local object.
I just tested the function without the $ argument and it works regardless.
Yes, but that is because you also have a global object $
. The idea of passing it in explicitly is to avoid having the function access the global scope.
Upvotes: 0
Reputation: 5307
It's shorthand for http://api.jquery.com/ready/
The argument $ is in fact jQuery - it's often used to avoid conflicts with some other global $ variable
See: http://api.jquery.com/jQuery/#jQuery3
Upvotes: 2