, JSON INTO Person, getFullName. , , . , , , , , ...
<html>
<head>
<script>
var json = '[ {"firstName": "John", "lastName": "Smith"}, {"firstName": "Nancy", "lastName": "Jones"} ]';
var persons = eval(json);
function Person(person) {
if (person) {
for(var prop in person)
this[prop] = person[prop];
}
}
Person.prototype.getFullName = function() {
return this.firstName + ' ' + this.lastName;
}
var people = new Array();
for(var i=0; i < persons.length; i++){
people[i] = new Person(persons[i]);
}
if(people[0].getFullName() !== 'John Smith')
alert('Expected fullname to be John Smith but was ' + people[0].getFullName());
</script>
</head>
</html>