These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Reducing global vars - bookmark event

dirk_engfer
Registered: Dec 5 2010
Posts: 2

I apply a setAction on bookmarks.
 
When I try to put all variables inside the object zoomSetter, then my code doesn't work.
 
But when I place the var. 'that' outside the object hence declaring it as a global var. it works fine.
 
Who can tell how to reduce the amount of global variables in a right manner?
 
Here the code being at Document-Level:
 
var that;
var zoomSetter ={
zoomer : function(mythisobj){
that = mythisobj;
var bmroot = that.bookmarkRoot;
var bmlength = bmroot.children.length;
for (var i=0;i < bmlength;i++){
var thebm = bmroot.children[i];
thebm.setAction("that.zoomType=zoomtype.fitW;");
}
} // End zoomer method
}; // End zoomSetter object
zoomSetter.zoomer(this);

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Your problem is one of scoping. Members of an object are accessed with the keyword "this". "this" always references the current object. For functions inside an object, "this" references the object.

For example:

var myObj = {
nCount:1,
Inc: function(){return ++this.nCount;}
}
myObj.Inc(); //Returns 2

Or you could do it like this

function Incrementer(nStart)
{
this.nInc = nStart;
this.DoInc = function(){return ++this.nInc};
}

var myInc = new Incrementer(3)
myInc.DoInc(); // Returns 4


In the functions, "this" is used to reference other members of the same object.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

dirk_engfer
Registered: Dec 5 2010
Posts: 2
Hi Thom,

many thanks for your quick answer. I guess I understand that matter, and I gonna play around with "this" in nested functions.

Have a good Christmas season
regards
Dirk