JavaScripting InDesign  


A sporadic discussion of my scripting exploits using JavaScript to drive Adobe InDesign.


 

Saturday, August 25, 2007

     

Methods hit wall



I've gone off attaching methods to core JavaScript classes. This week, I've had two scripts fail dismally because of it, so even though the format of working with:
Object.prototype.isInArray = function(myArray){
  for (var i=0; myArray.length > i; i++) {
    if(myArray[i] == this){
      return true;
    }
  }
  return false;
}
is so convenient
if (myObj.isInArray(myArray)) {
or with:
Array.prototype.contains = function(myString){
  for (var i=0; this.length > i; i++) {
    if(myString == this[i]){
      return true;
    }
  }
  return false;
}

if (myArray.contains(myObj)) {
They 
can really backfire and slap you in the face if you're doing work that involves creating objects or arrays and examining their contents. So, I'm back to the prosaic but safe:
function arrayContains(anArray, anItem) {
  for (var i = 0; anArray.length > i; i++) {
    if (anItem == anArray[i]) return true;
  }
  return false;
}

if (arrayContains(myArray, myObj)) {
Which most people 
would have been using all along anyway.


# posted by Dave Saunders @ 5:15 AM 


Comments:  
There are indeed several cases where additonal member functions can blow into your face - you should have mentioned a few.
This is only about the very basic classes, such as Object and Array. You can still benefit from member functions if you keep them confined to their own classes. They can also be helpful in the InDesign collection classes.

function MyArray() {}
MyArray.prototype.contains = function ...

# posted by Blogger Dirk Becker : 12:28 AM 
   

Firstly I would like to thank you for your site Dave as it has helped me out of a couple of tight spots. I think I have a solution to your problem which allows you to use prototypes without returning the prototyped object in your arrays and or objects - works for both!

function objectForInLoop(myObject){
 myArray = [];
 for(objectName in myObject){
 if(myObject.hasOwnProperty(objectName)) {//This line is the key it will ignore protypes
 var currentObj = myObject[objectName]
 myArray.push(objectName);
 }
 }
 return myArray;
}

# posted by Anonymous Dan Grant : 7:40 PM 
   

Dan: The problem here is that if you add a method to an object, you potentially break other people's code. Indeed, in your own scripts, you can work around the issue, but if you're cooperating with other people who don't know to use the work-around, their code can be broken by this.

That's what happened to me (seems a long time ago now) so I swore off doing this.

# posted by Blogger Dave Saunders : 4:21 AM 
   
Post a Comment  

<< Home  

This page is powered by Blogger. Isn't yours?



My Photo
Name: 

Location: Ocean, New Jersey, United States 


 


Links


Google News

Browse Archived Scripts
 
Previous Posts


Open a Copy

Tables and keystrokes

Active Document Gotcha.

Last shall be First versus NextItem

Adding a Column of Numbers

Get the Lead out!

Change of Heart about Kick-offs

More Edit Original

Edit Original Continued

Edit Original for Placed Pages
 
Featured Scripts Downloads


PopTabUnleashed

Make Booklet for CS3

Build Booklet for CS & CS2

Add Line to Group

Break Out Text Frame

Change Case of Selected Style

Divide Story into Two

Proxy Aware Align Graphic in Frame

Regex Tester

Smart Title Case

Text Styles Reports for CS2
 
Archives


August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
February 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
December 2006
January 2007
February 2007
April 2007
June 2007
July 2007
August 2007
September 2007
January 2008
July 2008
October 2008
January 2012