Actually it can be done in Rhino now, using that 'hide' function.

$ rhino
Rhino 1.7 release 2 2009 03 22
js> var hide = function (o, p) {
  >   java.lang.Class.forName
("org.mozilla.javascript.ScriptableObject")
  >     .getMethod("setAttributes", java.lang.String,
  > java.lang.Integer.TYPE)
  >     .invoke(o, p, new java.lang.Integer(
  >       org.mozilla.javascript.ScriptableObject.DONTENUM));
  > }
js> Object.prototype.hello = function() { print('hello, world') }

function () {
    print("hello, world");
}

js> hide(Object.prototype, 'hello')
js> var obj = {}
js> for (var p in obj) print(p)
js> obj.hello()
hello, world
js>

However, ES5 will allow you to do it in standard javascript using the
Object.defineProperty method.

js> Object.defineProperty(Object.prototype, 'hello',
{enumerable:false, value:function() { print('hello, world') }})
[object Object]
js> var obj = {}
js> for (var p in obj) print(p)
js> obj.hello()
hello, world
js>

HTH


On Jul 22, 8:07 am, Mark Porter <[email protected]> wrote:
> On Jul 21, 9:23 am, Rapha <[email protected]> wrote:
>
>
>
>
>
> > Okay, there's a couple of problems there:
>
> > Firstly, setAttribute (and thus the js function hide) only operates on
> > direct properties of the object passed in, not on anything further up
> > the prototype chain.
>
> > Secondly, setAttribute will create a property (with value null) if it
> > doesn't already exist when setAttribute is called, so to avoid that
> > you need to call hide *after* the property has already been set on the
> > object.
>
> > $ rhino
> > Rhino 1.7 release 2 2009 03 22
> > js> var hide = function (o, p) {
> >   >   java.lang.Class.forName
> > ("org.mozilla.javascript.ScriptableObject")
> >   >     .getMethod
> > ("setAttributes",java.lang.String,java.lang.Integer.TYPE)
> >   >     .invoke(o, p, new java.lang.Integer(
> >   >       org.mozilla.javascript.ScriptableObject.DONTENUM));
> >   > }
> > js> function Test() {}
> > js> Test.prototype.doStuff = function() { return 'stuff' }
>
> > function () {
> >     return "stuff";
>
> > }
>
> > js> var obj = new Test()
> > js>
> > js> hide(obj, 'doStuff')
> > js> for (var p in obj) print(p)
> > doStuff
> > js>
> > js> hide(Test.prototype, 'doStuff')
> > js> for (var p in obj) print(p)
> > js>
> > js> obj.a = 1
> > 1
> > js> for (var p in obj) print(p)
> > a
> > js> hide(obj, 'a')
> > js> for (var p in obj) print(p)
> > js>
>
> > On Jul 21, 11:50 am, Mark Porter <[email protected]> wrote:
>
> > > On Jul 20, 10:08 am, Mark Porter <[email protected]> wrote:
>
> > > > On Jul 20, 7:35 am, Martin Blom <[email protected]> wrote:
>
> > > > > And of course, DONTENUM can be used from JS too:
>
> > > > > var __dontenum__ = function (o, p) {
> > > > >   java.lang.Class.forName("org.mozilla.javascript.ScriptableObject")
> > > > >     .getMethod("setAttributes", java.lang.String, 
> > > > > java.lang.Integer.TYPE)
> > > > >     .invoke(o, p, new java.lang.Integer(
> > > > >       org.mozilla.javascript.ScriptableObject.DONTENUM));
>
> > > > > }
>
> > > > > On 07/20/2009 03:19 PM, Rapha wrote:
>
> > > > > > I'm working on implementing ES5 in Rhino. The Object.defineProperty 
> > > > > > is
> > > > > > currently implemented in the CVS version, 
> > > > > > seehttp://www.mozilla.org/rhino/download.html
>
> > > > > > In the meantime you can add non-enumerable properties from Java, 
> > > > > > using
> > > > > > the ScriptableObject.DONTENUM attribute.
>
> > > > > > On Jul 20, 1:15 am, Mark Porter<[email protected]>  wrote:
> > > > > >> Is there currently a way to define non-enumerable properties from
> > > > > >> Javascript? Something like ES5's defineProperty? If not, are there
> > > > > >> future plans to support this?
>
> > > > Wow, that is exactly what I needed. Thanks Rapha and Martin.
>
> > > > Rapha, I'll try to follow your blog to keep up with your progress.
>
> > > > Thanks,
> > > > Mark
>
> > > > ------------
> > > > Myna Server-Side Javascript:http://www.mynajs.org
>
> > > OK, any idea on how to hide properties on instantiated objects? I
> > > tried hiding the properties on the prototype, but that has no effect.
> > > Hiding the property inside the constructor seems to make it null:
>
> > > var hide = function (o, p) {
> > >   java.lang.Class.forName("org.mozilla.javascript.ScriptableObject")
> > >     .getMethod("setAttributes", java.lang.String,
> > > java.lang.Integer.TYPE)
> > >     .invoke(o, p, new java.lang.Integer(
> > >       org.mozilla.javascript.ScriptableObject.DONTENUM));
>
> > > }
>
> > > function Test (){
> > >    hide(this,"doStuff")
>
> > > }
>
> > > Test.prototype.doStuff=function(){ return "stuff"}
>
> > > new Test().doStuff();
> > > //TypeError: Cannot call property doStuff in object [object Object].
> > > It is not a function, it is "object".
>
> > > Is it possible to make instance functions non-enumerable?
>
> > > Thanks,
> > > Mark
>
> > > ------------
> > > Myna Server-Side Javascript:http://www.mynajs.org
>
> I really should have been clear from the start. What I would like to
> do is add functions to the Object prototype without breaking the
> classic for (var in obj) behavior. It looks like this just can't be
> done in Rhino right now. Will ES5 support change that?
>
> Thanks for your time,
> Mark

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

Reply via email to