Copy link to clipboard
Copied
I'm brand new.
When I'm looking at this forum sometimes I see this.getField("fieldname").xxx and sometimes I see getField("fieldname").xxx
When are you supposed to use which and why?
I see a lot more problems when "this" is used when not appropriate compared to omitting it when not appropriate. The former can lead to subtle bugs and the latter normally causes an immediately apparent error, which you then fix. I almost always omit it in most code that resides in a document, but almost never do in folder-level JavaScripts. I've never had a problem doing so in almost 20 years, and I've saved 5 bytes in every instance. I find the code much more readable when omitted, so for the
...Copy link to clipboard
Copied
The "this" object usually refers to the current Document object. If you don't specify it then the application uses it implicitly, but I don't think it's good practice. I would recommend always using it explicitly, to avoid possible problems and for the code to be more complete.
Copy link to clipboard
Copied
I see a lot more problems when "this" is used when not appropriate compared to omitting it when not appropriate. The former can lead to subtle bugs and the latter normally causes an immediately apparent error, which you then fix. I almost always omit it in most code that resides in a document, but almost never do in folder-level JavaScripts. I've never had a problem doing so in almost 20 years, and I've saved 5 bytes in every instance. I find the code much more readable when omitted, so for the most part it comes down to a matter of programming style.
Copy link to clipboard
Copied
Thanks George!
Copy link to clipboard
Copied
Agreed. It comes down to a question of personal style, like indenting your code or selecting names for your variables.
Different people have different preferences/customs. I also agree that it's more likely to go wrong if you don't use it in a folder-level script than a doc-level one, but I have seen cases where it does go wrong in the latter, specifically when you have a long chain of functions calling one another, which is why I prefer to err on the safe side.