• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

When are you supposed to use this.getField vs. getField and why?

New Here ,
Feb 01, 2017 Feb 01, 2017

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?

TOPICS
PDF forms

Views

7.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 01, 2017 Feb 01, 2017

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

...

Votes

Translate

Translate
Community Expert ,
Feb 01, 2017 Feb 01, 2017

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2017 Feb 01, 2017

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2017 Feb 02, 2017

Copy link to clipboard

Copied

Thanks George!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 03, 2017 Feb 03, 2017

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines