[thelist] JSON and Client-side classes

Mark Baldwin mark.baldwin at attensa.com
Tue Apr 4 17:50:22 CDT 2006


Thanks for taking up the cause. What you came up with is what I ended  
up doing. Turned out to be the only feasible option.
What I though would have been slick at the time would to be able to  
go like this:

var newGuy = eval('(' + jsonString + ')');

So with a little foresight (both server and client know about the  
object to create, security safeguards, etc.) a php script could send  
down a brand spanking new object to the client without the extra  
steps of creating a new object and then decorating it with the  
returned data.


Mark

On Apr 4, 2006, at 11:23 AM, sam foster wrote:

> 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
> -- 
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !




More information about the thelist mailing list