1 Reply Latest reply on Jul 29, 2011 12:35 AM by try67

    When do I (and don't I) use "this."?

    Sean McGilbert

      Whats the rule for using "this."?

       

      I see it regularly omitted from script samples. As in:

       

      getField('FancyName').display

       

      I would script the above as:

       

      this.getField('FancyName').display

       

      I use it all the time. Am I using it too much?

        • 1. Re: When do I (and don't I) use "this."?
          try67 MVP & Adobe Community Professional

          "this" can be omitted in all contexts when accessing its properties and

          methods (ie not when using it as a parameter in a function call, for

          example). If you're using it incorrectly, though, you will get an error

          whether you omitted it or not.

          It does no harm to use it and doesn't change the speed of the script, or

          anything like that. It can even make the code more readable.

           

          This is actually the reason why it's not a good idea to create variables or

          functions with identical names to those of the pre-defined objects. For

          example, if you try to define a variable called "path" in the console with a

          file opened in the background you'll get an error message: "redeclaration of

          const path", because the Document object already has such a property

          defined.

          If you close the file, then you could create your variable, but it will be

          overwritten as soon as a file is opened. Dangerous stuff...