[jQuery] Plugin - internal functions

[jQuery] Plugin - internal functions

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>Actually, the JSON plugin uses both techniques. It has a couple of
internal functions nested inside $.fn.json(), and it also has a static $.json
object. (That's an object, not a function, but it is the same question either
way.)<BR></FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>I seem to recall that I made $.json a property of the $ object so
that it would persist across calls to $().json(), but looking at the code again,
that may not have been necessary.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>I like nested functions because they don't take up any global namespace,
and because they can easily share data with their parent
function.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>But suppose you have a plugin with several methods that all call one
common utility method. That utility method would be a good candidate for the
$.foo = function(){} treatment, so that all your public methods can use
it.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>The JSON plugin has only a single public method, so there wasn't any need
to expose any of its internal functions.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>So, each approach may be best for different
situations.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656523913-09052006><FONT face=Verdana
size=2>-Mike</FONT></SPAN></DIV><BR>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> discuss-bounces@jquery.com
[mailto:discuss-bounces@jquery.com] <B>On Behalf Of </B>ashutosh
bijoor<BR><B>Sent:</B> Tuesday, May 09, 2006 3:06 AM<BR><B>To:</B> jQuery
Discussion.<BR><B>Subject:</B> [jQuery] Plugin - internal
functions<BR></FONT><BR></DIV>
<DIV></DIV>I was just reading about creating jquery plugins, when I noticed
the following comment in the docs:<BR>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<P>If you need to write any private methods (ones that the end-coder doesn't
need to see), you can feel free to bind them to the $ object itself, for
example: </P><PRE>$.test = function() {<BR> // Do some internal stuff<BR>};<BR></PRE>
<P>You can then access it in the same manner: </P><PRE>$.test("some stuff");<BR></PRE>
<P>We recommend that you do it this way so that your code doesn't
accidentally overwrite any other functions that the user may be using.
</P></BLOCKQUOTE>But if you see how <SPAN id=site-slogan>Michael has written
internal functions in his json plugin, he defines his internal functions
inside the plugin function itself.<BR>Would this not be the recommended way to
write internal functions?
<BR><BR>Regards<BR>Ashutosh<BR></BLOCKQUOTE></SPAN></BODY></HTML>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/




































































    • Topic Participants

    • mike