Article View: adobe.scripting.indesign
Article #8818Re: Javascript doubt
From: Dave_Saunders
Date: Sat, 23 Sep 2006 10:59
Date: Sat, 23 Sep 2006 10:59
26 lines
2267 bytes
2267 bytes
1. Square brackets '[0]' are used to provide an index into an array or a collection. Parentheses '(0)' indicate you are passing a value to a method as a parameter.
For example:
app.activeDocument.pages[0]
app.activeDocument.pages.item(0)
are equivalent (because, in spite of the reference guide, item is a method, not a property).
There is a major difference between arrays and collections in this regard: with arrays, you cannot access relative to the last item using a negative number while with collections you can. For this reason, some respected experts advocate always using the item() method when addressing a collection to remind yourself which you're dealing with. While I can see the validity of this, I fear that some deepseated laziness has me always using the square bracket notation.
Note that there are other places where parentheses and square brackets can be used for entirely different purposes.
2. The '{}' is a mechanism for declaring a literal object (sometimes called "records"). In the partial statement you provide, that particular add method accepts a properties object and that's how the parameter you provided is interpreted.
3. Short answer: no. JavaScript does have a "for ... in" construction that looks similar but is quite different. It provides a mechanism for iterating through the names of the properties of an object. This can be exploited using the square bracket syntax for accessing properties.
For example:
app.activeDocument.pages[0]
is equivalent to:
app["activeDocument"]["pages"][0]
Dave
Technical Details
Message-ID:
<3bc1bf71.0@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!213.132.189.2.MISMATCH!multikabel.net!feed20.multikabel.net!news.germany.com!news.isoc.lu!webcrossingReferences:
<3bc1bf71.-1@webcrossing.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,7de22d946ccdbc69 X-Google-Attributes: gidf939a,public X-Google-Language: ENGLISH,ASCII-7-bit From: Dave_Saunders@adobeforums.com Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!213.132.189.2.MISMATCH!multikabel.net!feed20.multikabel.net!news.germany.com!news.isoc.lu!webcrossing Newsgroups: adobe.scripting.indesign Subject: Re: Javascript doubt Message-ID: <3bc1bf71.0@webcrossing.la2eafNXanI> Date: Sat, 23 Sep 2006 03:59:10 -0700 References: <3bc1bf71.-1@webcrossing.la2eafNXanI> Lines: 42 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit