Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Learning JS — Can someone explain this function? Reply To: Learning JS — Can someone explain this function?

#150764
__
Participant

You’re assigning your function to $.fn.myFunction, but calling it like $.myFunction.

If your function does not operate on the dom, I would suggest assigning it to $ directly (or not using jQuery at all).

If you are interested in writing a jQuery plugin, remember that you’ll need to call your method on a jQuery object (i.e., something returned by $, not $ itself):

$().myFunction();

. . .

Also, inside your function, use this instead of $ to reference your variables: updated fiddle

. . . OR, make myGlobals a property of myFunction (unless there’s a really good reason not to – it’s best to use as few names as possible in $.fn): updated fiddle #2