Javascript prototype undefined after eval deserialization Javascript prototype undefined after eval deserialization json json

Javascript prototype undefined after eval deserialization


An object x that is created by x=new Person() is linked to/inherits from Person.prototype, but as far as the ecma standard is concerned you cannot change x.prototype in order to change that link/inheritance afterwards, that's the "magic power" only the new keyword possesses.
Mozilla seems to offer a way to change the object link after an object has been created through the non-standard property __proto__.

Mozilla-only:

//assign prototypefor(var i=0; i < people.length; i++){    people[i].__proto__ = Person.prototype;}

should work anywhere:

function Person(data) { this.data = data; }Person.prototype.getFullName = function() {    return this.data.firstName + ' ' + this.data.lastName;}eval('people = ' + json);//assign prototypefor(var i=0; i < people.length; i++){    people[i] = new Person(people[i]);}


The prototype property is a property of constructors, not of instances. What you are looking for is the property __proto__:

people[i].__proto__ = new Person();

The bad news is that it does not work in all browsers. It does work in Firefox and Safari, it does not work in IE. An alternative is to use constructors to instantiate your array of people. Unfortunately you'll have to copy all properties:

function Person(obj) {    for (var property in obj) {        this[property] = obj[property];    }    return this;}Person.prototype.getFullName = function() {    return this.firstName + ' ' + this.lastName;}var people;eval('people = ' + json);for(var i=0; i < people.length; i++) {    people[i] = new Person(people[i]);}


Basically, you have to get the JSON object INTO a Person object, and then the getFullName just applies. I've rewritten what you had slightly to work. There's probably even better ways, but I think this is what you were intending to do...

<html><head><script>//NOTE: Sending around JSON arrays leaves bad security holes for non-IE browsers (__defineSetter__)var json = '[ {"firstName": "John", "lastName": "Smith"}, {"firstName": "Nancy", "lastName": "Jones"} ]';//Persons is just a temporary JSON arrayvar persons = eval(json);//constructor takes optional object instance and copies all properties if it gets onefunction Person(person) {     if (person) {        for(var prop in person)        this[prop] = person[prop];    }}//Prototype applies to all Person objectsPerson.prototype.getFullName = function() {    return this.firstName + ' ' + this.lastName;}//Create People arrayvar people = new Array();for(var i=0; i < persons.length; i++){    people[i] = new Person(persons[i]);}//Now do your checkif(people[0].getFullName() !== 'John Smith')    alert('Expected fullname to be John Smith but was ' + people[0].getFullName());</script></head></html>