JSON 데이터를 deserialize하고 각 객체의 프로토 타입을 업데이트하고 공통 함수를 상속합니다.eval deserialization 후 자바 스크립트 프로토 타입이 정의되지 않았습니다.
그러나 다음 스크립트는 "people [0] .getFullName은 함수가 아닙니다."라는 오류를 발생시킵니다. deserialize 된 객체의 프로토 타입은 할당 후에 정의되지 않은 것처럼 보입니다.
<html>
<head>
<script>
var json = '[ {"firstName": "John", "lastName": "Smith"}, {"firstName": "Nancy", "lastName": "Jones"} ]';
var people;
eval('people = ' + json);
function Person() { }
Person.prototype.getFullName = function() {
return this.firstName + ' ' + this.lastName;
}
//assign prototype
for(var i=0; i < people.length; i++){
people[i].prototype = new Person();
}
if(people[0].getFullName() !== 'John Smith')
alert('Expected fullname to be John Smith but was ' + people[0].getFullName());
</script>
</head>
</html>
CTOR의 속성 복사 루프는 내가하려고하는 것에 대해 가장 늦은 바운드 해결책을 제공합니다. 고마워! –