Loading ...
Sorry, an error occurred while loading the content.

Re: Understanding j/s coding practices...

Expand Messages
  • calv_j
    Excellent - thanks so much Satyam, seems to make sense! I ll take a look at that article - thanks again! C. ... get ... be ... or ... is ... in this ... would
    Message 1 of 4 , Oct 12, 2007
    • 0 Attachment
      Excellent - thanks so much Satyam, seems to make sense! I'll take a
      look at that article - thanks again!

      C.

      --- In ydn-javascript@yahoogroups.com, "Satyam" <satyam@...> wrote:
      >
      > Everything will be clear once you read Eric's article:
      >
      > http://yuiblog.com/blog/2007/06/12/module-pattern/
      >
      > anyway, I wrote some explanations below.
      >
      > ----- Original Message -----
      > From: "calv_j" <calv_j@...>
      > To: <ydn-javascript@yahoogroups.com>
      > Sent: Friday, October 12, 2007 1:10 AM
      > Subject: [ydn-javascript] Understanding j/s coding practices...
      >
      >
      > > Hi all - if anyone could give me a quick pointer so I can try and
      get
      > > my head around why certain coding practices are getting used I'd
      be
      > > very grateful...
      > >
      > > OK -
      > >
      > > What would be the difference (or point of doing it different...) -
      or
      > > is it just matter of preference - of these 2:
      > >
      > > var NS = window.NS || {};
      > >
      > > NS.utils = function(){
      > >
      > > return {
      > > foo: "baa",
      > > doSomething : function(_prop){
      > > // code
      > > },
      > > doSomethingElse : function(){
      > > // code
      > > }
      > > }();
      > >
      > > or
      > >
      > > NS.utils = {
      > > foo: "baa",
      > > doSomething : function(_prop){
      > > // code
      > > },
      > > doSomethingElse : function(){
      > > // code
      > > }
      > > }
      > >
      > > i.e. both result in an object with the same properties etc. - so
      is
      > > there any reason to choose between the 2?
      > >
      >
      > The first way would allow you to define private variables, though
      in this
      > case it doesn't. Variables declared in between these two lines
      would be
      > accessible to the functions within NS.utils but not visible outside:
      >
      > NS.utils = function(){
      > var thisIsPrivate;
      > return {
      >
      > > //
      > >
      > > What's the following about? It's a snipped down chunk from a Yahoo
      > > library - an annoymous function, called at the end as per above,
      not
      > > returning anything this time... i.e. why wrap with the function?
      >
      > Same thing as before: private variables. Here variables Overlay
      and
      > OverlayManager are private, they can be seen by any function
      defined within
      > the anonymous function but not outside. They both point to
      globally
      > available variables so their only purpose is to provide handy
      shortcuts to
      > those same objects, without using the global namespace. You will
      often see
      > code that starts like this:
      >
      > (function () {
      > var $D = YAHOO.util.Dom,
      > $E = YAHOO.util.Event,
      > $L = YAHOO.lang,
      > $ = $D.get;
      >
      > All those $something are not PHP, $ is a valid character in a name
      and they
      > are shortcuts to some often used library components and or
      methods. So,
      > instead of writing:
      >
      > YAHOO.util.Dom.get('something').innerHTML = '';
      >
      > you can do
      >
      > $('something').innerHTML = '';
      >
      > It is faster both for you and for the interpreter.
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.