2
\$\begingroup\$

Is there a better way to do this?

function Person(name)
{
    this._type='Person';
    this.name=name;

    this.hello = function(){
        alert('Hello '+this.name);
    };
}

function object_to_instance(key, value)
{
    if (!value.hasOwnProperty('_type'))
        return value;

    var obj= eval('new '+value._type+'()');

    for (var property in value)
        obj[property]=value[property];
    return obj;
}

var people = [new Person('Harry'), new Person('Sally')];

var people_json = JSON.stringify(people);

var new_people = JSON.parse(people_json, object_to_instance);
new_people[0].hello();
\$\endgroup\$
1
  • \$\begingroup\$ instead of eval you could do: var obj = new window[value._type](value.name); (I think you could at least; I don't use classes in js). \$\endgroup\$ – Bill Barry May 5 '12 at 1:32
2
\$\begingroup\$

Instead of eval, you could create a Person constructor that accepts an object:

function Person(arg)
{
    if (typeof arg === 'string')
    {
        var name = arg;
        this._type='Person';
        this.name=name;
    }
    else if (typeof arg === 'object')
    {
        var value = arg;
        for (var prop in value)
        {
            if (value.hasOwnProperty(prop))
            {
                this[prop] = value[prop];
            }
        }
    }
}

// Move Person.hello() into the prototype
Person.prototype.hello = function()
{
    alert('Hello '+this.name);
};

Then use the _type to choose a constructor based on a predefined set of classes, rather than a (potentially) unsafe eval:

var classes =
{
    Person: Person
    /* other potential classes to deserialize from here as well */
};

function object_to_instance(key, value)
{
    if (!value._type) return value;
    return new classes[value._type](value);
}

var people = [new Person('Harry'), new Person('Sally')];
var people_json = JSON.stringify(people);
var new_people = JSON.parse(people_json, object_to_instance);
new_people[0].hello();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.