On 3月25日, 下午9时26分, Norris Boyd <[EMAIL PROTECTED]> wrote:
> On Mar 24, 10:42 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > When I try to overwrite the get function of a class, amazing thing
> > happens.
>
> > public class Counter extends ScriptableObject{
> > public Counter(){
> > System.out.println(this);}
>
> > ...........
> > private String id;
>
> > public void setId(String id){
> > this.id = id;
>
> > }
>
> > public void jsSet_id(String id){
> > this.id = id;}
>
> > public String jsGet_id(){
> > return this.id;}
>
> > public Object get(String name, Scriptable start){
> > System.out.println("Counter get "+name);
> > Object obj = super.get(name, start);
> > if(obj!=Scriptable.NOT_FOUND)
> > return super.get(name, start);
> > System.out.println("this "+this);
> > System.out.println("start "+start);
>
> > }
>
> > .............
>
> > }
>
> > The test code like this:
> > Object[] arg = { new Integer(7) };
> > Scriptable myCounter = cx.newObject(scope, "Counter",arg);
> > scope.put("c", scope, myCounter);
> > Counter counter = (Counter)myCounter;
> > counter.jsSet_id("id1");
> > The js code is very simple ,just like this:
> > c.id;
> > After running it, I get the following results:
>
> > " [EMAIL PROTECTED]
> > [EMAIL PROTECTED]
> > Counter get id
> > this [EMAIL PROTECTED]
> > start [EMAIL PROTECTED]
>
> > Counter get id "!
>
> > Now I want to know why the "
> > Counter get id " appears twice? I think It only appear once!
>
> > When I write the js code as this
> > c.id1
> > The result is like this
>
> > " [EMAIL PROTECTED]
> > [EMAIL PROTECTED]
>
> > Counter get id1
> > this [EMAIL PROTECTED]
> > start [EMAIL PROTECTED]
>
> > Counter get id1
> > this [EMAIL PROTECTED]
> > start [EMAIL PROTECTED]
> > "
> > Why there exist two objects, [EMAIL PROTECTED] object just keep
> > the class level attribute? Can you explain this simply for me?
> > Thank you!
>
> It's not clear from your post how "Counter" is defined. I'm assuming
> that you're using ScriptableObject.defineClass or something similar.
> These methods that define your class will create Counter.prototype,
> which is likely the "other" object that you see in your debug output.
> Lookup in JavaScript walks the prototype chain, so likely the
> mysterious "Counter get id" is a result of get() being called on the
> "c" object, and this its prototype.
>
> Incidentially, for most purposes it's easiest just to define the
> JavaScript objects you want as normal Java objects and then expose
> them to JavaScript using Context.javaToJS. The only feature you can't
> get from this route is dynamic properties.
>
> --N
Thank you very much!
The dynamic properties is just what I want. I am building a browser
javascript parser,
so most of the browser host objects should have the dynamic
properties,
BinLI
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino