[thelist] JSON and Client-side classes

sam foster potatosculptor at gmail.com
Tue Apr 4 13:23:12 CDT 2006


You could get the data to create your Person instance from a JSON
string.. is that what you are asking?

PersonObject = function(props) {
  for(var key in props) {
   this[key] = props[key];
  }
}

var person1 = new PersonObject( parseJSON(someJsonString) );

which is just a simplistic example. I prefer this model of passing
arguments into a construtor anyway. You could support either by
switching on the length of arguments, and/or the typeof the first
argument.

hth
Sam

On 3/7/06, Mark Baldwin <mark.baldwin at attensa.com> wrote:
> Greetings all,
>
> I've been playing around with JSON and I can't seem to find the
> answer to the following:
>
> Lets say you have a JS include that defines an object such as
>
> PersonObject = function() {
>         this.name = '';
>         this.age = '';
> }
>
> Is it possible, via JSON, to eval a string such that it would create
> a new instance of a person object? A bonus would be a way of doing it
> without creating an incredibly large, and obscene, constructor.
>
> Thanks in advance,
> Mark



More information about the thelist mailing list