Pages

Tuesday, August 28, 2018

Everythings an Object

Everythings an Object


One of the powers of JavaScript is that just about anything you work with is either an object or a property of an object. What�s more, some properties can themselves be objects too, so you end up with a hierarchy of objects. To take a simple example, consider the File System. Here, the objects are the files and folders of your file system.

By The Way: Objects vs. Object Model

    It takes some getting used to, separating the concept of an object model from the actual objects you work with. An object model defines how the objects of a "universe" relate to each other and what their properties are. An object is a particular object within the specific instance of its object model that you happen to be working with.

    For example, the InDesign object model says that a property of the application object is that it has a collection of documents. But, if you are not running InDesign, then you dont have an actual application object for your scripts to operate upon. Or, if you have no documents open, then the collection of documents for your InDesign at this moment is empty.
But heres an irony. You can write a lot of scripts (really, useful scripts) without ever realizing that a string is an object (in the JavaScript object universe). When I first started scripting InDesign with JavaScript, I was blissfully ignorant of this. It wasnt until I discovered the Math and Date objects that it started to click with me that strings are objects too.

Once I realized this, it opened my eyes to a new world of possibilities. For example, it allowed me to create a new method for my strings that mimics the AppleScript construct is in. In AppleScript (which I used to script InDesign 1.5 and 2.0 before JavaScript became available), a very useful feature was the ability to ask if a string was in an array of strings using something like:
    If myString is in myString Array then ...
In truth, this feature has broader applicability than for just strings albeit that 99% of the time I used it I would be dealing with strings.

Having grasped this, I created the following definition of a new method for strings in my script:
 String.prototype.isInArray = function(myArray){
  for (var i=0; myArray.length > i; i++) {
   if(myArray[i] == this){
    return true;
   }
  }
  return false;
 }
Armed with this, I could write statements like:
 If (myStyleName.isInArray(myDoc.paragraphStyles.everyItem()) {
This code snippet tests to see if the document referenced by the variable I named "myDoc" includes a paragraph style with the name contained in the variable I named "myStyleName".

It wasnt long before I realized that this method could be generalized to work with any object by simply changing the declaration to:
 Object.prototype.isInArray = function(myArray){
While it is rare that I use this method with anything other than a string, the possibility is always there.

Well, even though its Saturday morning, I have work I must attend to, so thats it for now.

visit link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.