[Javascript] Where to learn JS best practices & style

David T. Lovering dlovering at gazos.com
Sat Mar 29 01:12:40 CST 2003



> are you saying that the statement "document.all.myobject.value" is more
> efficiently expressed as "myobject.value"?

document.all points to everything inside the document, which is a waste of CPU and memory to cache all the
ID references.  It is only used to keep the DOM nazi's happy.  Real people only use it when they want to
confuse newbies.

Leaving off the form identifier can be bad, and one should never take the 'document' preamble in vain.  Be
a good DOMite, and try

  document.formName.myobject.value, making appropriate name substitutions.

> 
> how about document.all["obj" + lnJ++].value?  is there another way to
> reference a contruct like that?

Let me guess -- you must have been a C++ programmer in an earlier existence.

If you REALLY want to go skating down all the elements inside a form, try

  for (var i=0; i<document.myForm.elements.length; i++) {
    var myElement = document.myForm.elements[i];
    if (myElement.value != 'undefined') {
      var myValue = myElement.value;
    }
  }

Note that there are MANY JavaScript objects which are perfectly useful, but for
which '.value' is potentially meaningless.  This checks to make sure you are dealing with a
nice friendly object before it tries to suck up its value.

> 
> how different is document.all.myobject.value from
> myObjArray["row6"]["col5"].value?  Maybe i'm relying too much on VB/VFP's
> object model where the dotted referencing is just the nomenclature for
> explicitly stating the object heirarchy.  (aside from the fact that a DHTML
> document doesn't really have a heirarchy)

Quite different.  One is a static object, and the other is an array which requires unpacking.

As to the 'correct' books to read -- rule #1: discard everything printed by Microsoft.

Rule #2: Discard anything with the words 'Idiot' or 'Dummy' in the title.

Rule #3: Anything which bills itself as the 'complete' JavaScript which is less than 3 inches thick
can also be discarded.

All levity aside, the three references I use most often are

	JavaScript Developer's Dictionary (SAMS)
    PURE JavaScript (SAMS)
    Professional JavaScript (2nd Edition) (Wrox)

I would have recommended the JavaScript Bible, but the latest edition is nearly useless.
If you can find a Third Edition, buy it, but otherwise stick to the other 3 books.  I do have about 200 others, but their utility is usually limited to one or two pet topics near and dear to the heart of their author.

Also, when you see people leaving off prefixes like 'document' and 'window', rest assured that
they are lazy bastards who will eventually be sent to the hot place and forced to reinsert them into
the code written by other lazy bastards.

> 
>   re: removing all the comments/short names- Are you suggesting that a
> source-code be maintained with comments, and a production version without
> comments?  Do you think the overhead of delivering in-line comments
> outweighs the benefit of future readability/maintenance?  (consider the
> target being an Intranet client with at least 10/100Mb wire-speed)  In this
> same vein, do shorter (though perhaps less obvious) variable names improve
> performance over more immediately readable (and somewhat "self-documenting")
> variable naming conventions?  Isn't this why we've moved from 3rd to 4th
> generation development languages?  By the time i've reduced my code to 3
> character variable names without commenting, i might as well instantiate a
> compiled object.
> 

short names, particularly for temporary variables or indices are perfectly OK.
The awkwardness is derived from describing the length of the text variable myFieldName
in formA as merely being 'l'.  Five years from now you won't have a clue what 'l' is...
whereas myFieldNameLength is perfectly obvious.

I don't believe comments have any measurable impact on execution speed -- I believe
the JavaScript in-line compiler nukes them before running the remaining code.  Anyone who
wants to correct this naive delusion is welcome to step in anytime.

-- Dave Lovering

>   I'm hoping this thread goes towards a discussion about javascript rather
> than code examples of a solution.
> 
> thanks for your input in advance...
> 
> Michael_Dougherty at pbp.com
> 
> -----Original Message-----
> From: javascript at LaTech.edu [mailto:javascript at LaTech.edu]
> Sent: Friday, March 28, 2003 4:04 PM
> To: javascript at latech.edu
> Subject: Re: [Javascript] required entry field
> 
> No need for stabs, we are all exchanging experience between this
> memberhood.
> The question was addresed to Rodney as he sugested other way of salving
> the
> guy's problem.
> 
> I don't see no need to call the form collection in order to access a
> single
> field. Why ask browser to count items of collection once again
> when you can directly and selectively retrieve its properties.
> 
> Scripters should avoid using calls like 'document.all', allways and
> regularly. Give some considerations in optimizing their scripts, the speed
> of execution, size of the script, removing all the comments, using short
> declarations, variable names, shorter object calls and other, to achieve
> faster downloads, and faster executions.
> 
> Though this prism,it's contraproductive calling collections of any kind
> when
> items have given Names or IDs.
> 
> Regards!
> 
> >>From: "David T. Lovering" <dlovering at gazos.com>
> >>Reply-To: "[JavaScript List]" <javascript at LaTech.edu>
> >>To: "[JavaScript List]" <javascript at LaTech.edu>
> >>Subject: Re: [Javascript] required entry field
> >>Date: Fri, 28 Mar 2003 11:15:41 -0700
> >>
> >>Hmmm... I'll take a stab at this.
> >>
> >>Inputs and other data-entry fields and widgets exist solely to collect
> >>information from the remote user.  In order for them to work in this
> >>capacity, they are collectively grouped together under the heading of a
> >>'form', and are sent back to wherever the
> >>data processing and analysis is done (usually on a CGI/BIN formserver
> >>somewhere).
> >>
> >>Many of these widgets don't work at all, or don't work properly UNLESS
> >they
> >>are in a form.  The general structure of a form is as follows:
> >>
> >>   <body>
> >>   ... yada yada
> >>     <form name='myForm' action='stuff that points to formserver'
> >>type="CGI/BIN" method="POST">
> >>       <table>
> >>         ... tidy graphical arrangement of input devices
> >>       </table>
> >>     </form>
> >>   ... yada yada
> >>   </body>
> >>
> >>   Obviously, a lot more can be said.  If you want to use the form only
> >for
> >>the active data widgets and not to actually go anywhere, you can throw
> >away
> >>the type and method declaractions, and change the action as follows:
> >>
> >>   action='javascript:void(null)'
> >>
> >>   There, does that answer your question?
> >>
> >>   -- Dave Lovering
> >>
> >>BEKIM BACAJ wrote:
> >> >
> >> > Thanks, Rodney.
> >> > Now,I would like to ask a question on what 'form' stands for?
> >> > Also are you sugesting that my script will not work faster than
> >your's,
> >>or,
> >> > that my script will not work at all?
> >> >
> >> > Bekim
> >> >
> >> > >From: Rodney Myers <rodney at aflyingstart.net>
> >> > >Reply-To: "[JavaScript List]" <javascript at LaTech.edu>
> >> > >To: "[JavaScript List]" <javascript at LaTech.edu>
> >> > >Subject: Re: [Javascript] required entry field
> >> > >Date: Fri, 28 Mar 2003 10:11:19 +0000
> >> > >
> >> > >That would be .....
> >> > >
> >> > >function isValid(form) {  if(form.NeverBlank.value=='')
> >> > >   {
> >> > >    form.NeverBlank.focus()
> >> > >   }
> >> > >}
> >> > >
> >> > >hth
> >> > >Rodney
> >> > >
> >> > >
> >> > >
> >> > >BEKIM BACAJ wrote:
> >> > >
> >> > >>Einstein went mad when he heard that somebody wrote SF novel about
> >a
> >>man
> >> > >>who traveled with the speed of light and saw the batle of Waterloe,
> >>for it
> >> > >>is insaine he yelled.
> >> > >>But 'you' can init the new battle at Waterlloe again
> >> > >>
> >> > >>function isValid() {    if(NeverBlank.value==''){
> >> > >>         NeverBlank.focus()
> >> > >>                 }
> >> > >>         }
> >> > >>
> >> > >>dejavoo - loops forever - if the user does not supply some chars!
> >> > >>
> >> > >
> >> > >--
> >> > >PS. I have just bought a licence for Mike Chen's
> >> > >BizAutomator. If you have to answer routine email queries from your
> >>sites
> >> > >or mailings then
> >> > >you need this as much as I do. http://www.BizAutomator.com/rlbm51
> >> > >
> >> > >Rodney
> >> > >
> >> > >
> >> > >
> >> > >
> >> > >_______________________________________________
> >> > >Javascript mailing list
> >> > >Javascript at LaTech.edu
> >> > >https://lists.LaTech.edu/mailman/listinfo/javascript
> >> >
> >> > _________________________________________________________________
> >> > Protect your PC - get McAfee.com VirusScan Online
> >> > http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
> >> >
> >> > _______________________________________________
> >> > Javascript mailing list
> >> > Javascript at LaTech.edu
> >> > https://lists.LaTech.edu/mailman/listinfo/javascript
> >>_______________________________________________
> >>Javascript mailing list
> >>Javascript at LaTech.edu
> >>https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> _________________________________________________________________
> Add photos to your messages with MSN 8. Get 2 months FREE*.
> http://join.msn.com/?page=features/featuredemail
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript


More information about the Javascript mailing list