On Jul 21, 12:48 pm, Charles Lowell <[EMAIL PROTECTED]> wrote:
> Hi there,
>
> I'm considering using Rhino to create a behavior driven unit testing
> framework for java classes, but I cannot figure out how to add dynamic
> properties to java instances and/or extend java instance prototypes.
>
> For example, I would like to define something like
>
> Object.prototype.shouldEqual = function(that) {
>   if (this != that) {
>     throw new Error('<' + this + '> should equal ' + that)
>   }
>
> }
>
> to work for both instances of javascript objects as well as for
> instances of java classes?
>
> so That I might be able to say
>
> new java.lang.String('foo').shouldEqual(new java.lang.String('bar'))

Unfortunately, this approach won't work. Rhino doesn't reflect Java
objects into JavaScript using the full prototype heirarchy, so adding
properties to Object.prototype won't affect Java objects. The
rationale for this is that we wanted Java objects used in JavaScript
to act as close as possible to the way they act in Java. We also
wanted to avoid ambiguity: imagine the difficulty of figuring out what
should happen when calling toString, since that is defined as a method
of java.lang.Object and as a function property of Object.property.

However, you could certainly do some magic with a WrapFactory to get
these properties added if you wanted.

--N
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to