Casting plain objects to class instances in javascript

Viewed 23047
function Person() {
      var self = this;

      self.personName="";
      self.animals=[];
}

function Animal(){
     var self=this;

     self.animalName="";
     self.run=function(meters){
         .....
     }
}

Server response:

 [{personName:John,animals:[{animalName:cheetah},{animalName:giraffe}]} , {personName:Smith,animals:[{animalName:cat},{animalName:dog}]} ]

I'm getting Person array from server. I want to cast generic Person array to typed Person array. So I can use

 persons[0].Animals[2].Run();

I founded Javascript's

 Object.create(Person,person1);

But I want cross-browser version of it with array support

  ObjectArray.create(Person,persons);

or

 Object.create(Person[],persons);
4 Answers
Related