Skip to main content
Participating Frequently
February 1, 2017
Answered

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

  • February 1, 2017
  • 2 replies
  • 10967 views

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?

This topic has been closed for replies.
Correct answer George_Johnson

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.

2 replies

George_JohnsonCorrect answer
Inspiring
February 2, 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 most part it comes down to a matter of programming style.

Participating Frequently
February 2, 2017

Thanks George!

try67
Community Expert
Community Expert
February 1, 2017

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.