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?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #150706

    Hi there,

    I’m in the process of learning javascript and discovered an issue with a function I’m trying to call. The problem seems to be related to the object scope (I think) , but perhaps I’m missing something else. Below is a basic code sample for reference.

    Note: I’ve noticed the function executes if the variables are defined within it, but there should still be a way to get this working using the provided sample.

    I would appreciate any help/info on the subject. Thank You.



    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):


    . . .

    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

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.