[Project_owners] Javascript (de)serialization question

Eric H. Jung grimholtz at yahoo.com
Sun Mar 12 15:52:58 EST 2006

Hi Paul, thanks for the reply. Comments below...

--- Paul Tomlin <paul at paultomlin.com> wrote:

> Eric H. Jung wrote:
> > I cannot call p.foo(). This makes sense because toSource() doesn't appear to output functions
> > defined on an object. Is there anyway to make this work? How do you use toSource()/eval() on
> your
> > own classes?
> When you eval() the result of toSource(), is the resultant 'instanceof 
> foxyproxy.Proxy' true?

Yes, and so is foxyproxy.Proxy instanceof Object... as I'd expect.

> If not then I'd check that the class is being included in the toSource() 
> output.

Classname is not included in toSource().

> I don't think it's reasonable to expect the language to toSource() 
> method definitions in this way. Or more likely the _real_ cause is that 
> foo is not a property of 'p', it's a property of the prototype..

At this point, I don't have any expectations. I'm just exploring, trying to use toSource()/eval()
as a means to persist data instead of going the traditional RDF, custom XML, or preferences route.
If I can get it right, I sense that it will translate into hundreds of lines of less code than one
of those traditional alternatives.

I've seen it hinted here and there that other people use this technique in their extensions, so
I'm definitely not trailblazing.

> If initially you do
> p = new foxyproxy.Proxy();
> p.foo = function () { blah };
> alert( p.toSource() );
> do you then see 'foo' included in the toSource() output?

Yes. And here you've hit the nail on the head: only functions on the object (not the prototypical
object) are output in toSource(). That's a letdown because I'll have to duplicate these functions
for multiple objects, consuming more memory than necessary (i.e., I can't leverage the prototype
anymore). Fortunately, we're only talking about a few functions and a small number of objects.

> If so then 
> you're back to ensuring the class is defined prior to eval(), and that 
> toSource() indeed includes enough info about what class the object 
> belongs to.
> Persistence of class/meta data is not the same as persistence of 
> instance/object data :)

For most other languages I would agree with that statement, but I was under the impression that
Javascript didn't distinguish between objects and classes.

> On an entirely different matter, my next commit will be along the lines 
> of "finally managed to get prefs, scripts and templated menus working". Yay!

I haven't read that thread yet--it's sitting in my inbox :)  But glad to hear you solved it.
Thanks for asking me the right questions! If anyone else has feedback, I'd really like to hear it.


More information about the Project_owners mailing list