5

试图反序列化 JSON 数据并更新每个对象的原型并继承一个通用函数。

但是,以下脚本会引发错误“people[0].getFullName is not a function”。分配后反序列化对象的原型似乎未定义。

<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>
4

4 回答 4

4

由 x= new Person()创建的对象 x链接到/继承自 Person.prototype,但就 ecma 标准而言,您不能更改 x.prototype 以在之后更改该链接/继承,这就是“魔力”只有关键词才有。
Mozilla 似乎提供了一种在通过非标准属性__proto__创建对象后更改对象链接的方法。

仅限 Mozilla:

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

应该在任何地方工作:

function Person(data) { this.data = data; }
Person.prototype.getFullName = function() {
    return this.data.firstName + ' ' + this.data.lastName;
}

eval('people = ' + json);
//assign prototype
for(var i=0; i < people.length; i++){
    people[i] = new Person(people[i]);
}
于 2009-07-05T21:32:41.047 回答
2

prototype属性是构造函数的属性,而不是实例的属性。您正在寻找的是财产__proto__

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

坏消息是它不适用于所有浏览器。它在 Firefox 和 Safari 中有效,在 IE 中无效。另一种方法是使用构造函数来实例化您的人员数组。不幸的是,您必须复制所有属性:

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]);
}
于 2009-07-05T21:34:08.657 回答
2

基本上,您必须将 JSON 对象转换为 Person 对象,然后才应用 getFullName。我已经重写了你需要做的工作。可能有更好的方法,但我认为这就是你打算做的......

<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 array
var persons = eval(json);

//constructor takes optional object instance and copies all properties if it gets one
function Person(person) { 
    if (person) {
        for(var prop in person)
        this[prop] = person[prop];
    }
}

//Prototype applies to all Person objects
Person.prototype.getFullName = function() {
    return this.firstName + ' ' + this.lastName;
}

//Create People array
var people = new Array();
for(var i=0; i < persons.length; i++){
    people[i] = new Person(persons[i]);
}

//Now do your check
if(people[0].getFullName() !== 'John Smith')
    alert('Expected fullname to be John Smith but was ' + people[0].getFullName());

</script>
</head>
</html>
于 2009-07-05T21:36:34.910 回答
2
for(var i=0; i < people.length; i++){
      people[i].getFullName = Person.prototype.getFullName; }
于 2009-07-05T21:37:29.993 回答