views:

1039

answers:

3

First off: I'm using a rather obscure implementation of javascript embedded as a scripting engine for Adobe InDesign CS3. This implementation sometimes diverges from "standard" javascript, hence my problem.

I'm using John Resig's jsdiff library (source here) to compare selections of text between two documents. jsdiff uses vanilla objects as associative arrays to map a word from the text to another object. (See the "ns" and "os" variables in jsdiff.js, around line 129.)

My headaches start when the word "reflect" comes up in the text. "reflect" is a default, read-only property on all objects. When jsdiff tries to assign a value on the associative array to ns['reflect'], everything explodes.

My question: is there a way around this? Is there a way to do a hash table in javascript without using the obvious vanilla object?

Ground rules: switching scripting engines isn't an option. :)

A: 

Well given objects in javascript are just associative arrays, there really isn't another built in solution for a hash. You might be able to create your own psuedo hashtable by wrapping a class around some arrays although there will probably be a significant performance hit with the manual work involved.

Just a side note I haven't really used or looked at the jsdiff library so I can't offer any valid insight as per tips or tricks.

Quintin Robinson
+5  A: 

You might be "asking the wrong question" (as Raymond Chen would say); rather than trying to avoid using the vanilla objects, try changing the way the associative array members are named.

The way I'd try to approach this: instead of there being an array member ns["reflect"], change the way that jsdiff builds the arrays so that the member is ns["_reflect"] or some other variation on that.

delfuego
+1. Clever! ... :-)
paercebal
I smacked my forehead shortly after asking the question. This, of course, did the trick. Thanks!
David Eyk
+1  A: 

If the JS implementation you're using supports the hasOwnProperty method for objects, you can use it to test whether a property has explicitly been set for an object or the property is inherited from its prototype. Example:

if(object.hasOwnProperty('testProperty')){
     // do something
}
Joel Anair