Adobe acrobat forms javascript object specification User Manual

Page of 64
Acrobat Forms - JavaScript Object Specification
52
this
Object
In JavaScript the special keyword “this” refers to the current object. In Acrobat Forms the
current object is defined as follows:
• In an object method, it is the object to which the method belongs.
• In a constructor function, it is the object being constructed.
• In a function defined at the
it is undefined. It is recommended
that calling functions pass the document object to any function at this level that
needs it.
• In a
script or
script it is the document object and
therefore can be used to set or get document properties and functions.
For example, assume that the following function was defined at the Plug-in folder level:
function PrintPageNum(doc)
{ /* Print the current page number to the console. */
console.println(“Page=” + doc.page);
}
The following script outputs the current page number to the console (twice) and then prints the
page:
PrintPageNum(this);
/* Must pass the document object. */
console.println(“Page=” + this.pageNum);
/* Same as the previous call. */
this.print(false, this.pageNum, this.pageNum); /* Prints the current page. */
Variable and Function Name Conflicts
Variables and functions that are defined in scripts are parented off of the this object. For
example:
var f = this.getField(“Hello”);
is equivalent to
this.f = this.getField(“Hello”);
with the exception that the variable “f” can be garbage collected at any time after the script is
run.
If a script uses a name that conflicts with a document level property or method and the this
object is the current document then a run-time error will result: Invalid or unknown property,
set not possible
.
The following script fragment will generate such an error:
var pageNum = this.pageNum;
To avoid such an error, rename your variables and functions to avoid conflict:
var pNum = this.pageNum;