Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.straub-nv.de!noris.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <8932764.SEqChMirdb@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Mon, 14 Nov 2011 13:15:55 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: Difference between findPos("divThis") and findPos(divThis) Newsgroups: comp.lang.javascript References: <4775241.ypaU67uLZW@PointedEars.de> <2394720.ArG6xLiZAS@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 67 NNTP-Posting-Date: 14 Nov 2011 13:15:56 CET NNTP-Posting-Host: 71a5590c.newsspool3.arcor-online.net X-Trace: DXC=GAmIG8WS2O7;]cDoEWD6A4McF=Q^Z^V384Fo<]lROoR18kF On 13/11/2011 20:10, Cal Who wrote: >>> >>> var el = (typeof obj == 'string') ? document.getElementById(obj) : >>> obj, >>> w = el.offsetWidth, // , instead of ; >>> h = el.offsetHeight; >>> >> That's neat. >> >> Except for the commas! >> >> I looked up comma operator but am still confused. >> >> Wouldn't this be OK (better?) >> >> var el = (typeof obj == 'string') ? document.getElementById(obj) : obj; >> w = el.offsetWidth ; >> h = el.offsetHeight; >> ... >> > > No, because the ";" marks the end of the variable declaration list. In > the above code, you are indeed creating an implied global variable > called h, No, it creates, if it succeeds (and does not throw an exception, as observed in some instances with MSHTML before, and required in ECMAScript Ed. 5 strict mode), a property with that name of the ECMAScript Global object or overwrites a property with that name of an object in the scope chain. > instead of creating a private variable in the function scope. The proper terminology here is "_local_ variable", not what you wrote. Visibility modifiers do not exist in these implementations; privacy can only be achieved implicitly, through closures. So far there is no closure in that code regarding those variables. > it is one of the JavaScript best practices to declare all of the > variables used in a function at the top of the function body. You may > have many variables separated by commas, e.g > > var el = ..., > h = ..., > w = ..., > i, j, len, ..., z; > > You must not use multiple var statements such as: > var el; > var h = ...; > var z; Wrong. In fact, it turns out that syntactically *correct*, adjacent `var' statements work better with editors capable of refactoring, such as Eclipse JSDT. And they help to avoid the problem that you encountered, because it would likely be a syntax error if you mistyped the semicolon, and automatic semicolon insertion would take place if you forgot to type the semicolon at all. PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee