2015-10-24 49 views
0

我正在研究一個Person構造函數,它將名稱和年齡作爲其參數,並試圖實現一種方法來檢索當前年齡值和輸出的所有'Person'實例平均。這裏是我的代碼...檢索在一個對象的所有實例中保存的值

var Person = (function() { 
    //private state 
    var inst = 1; 

    function Person(name, age) { 
      this.name = name; 
      this.age = age; 
      Object.defineProperty(this, "age", { 
       get: function() { 
        return age; 
       }, 
       set: function(num) { 
        age = num; 
       } 
      }); 
      Object.defineProperty(this, "_id", { 
       value: inst++ 
      }); 
     } 

    //Attempt to return number of instances divided by all current Person weights 
    Person.prototype.aveAge = function() { 

     return inst; 
    }; 
    return Person; 
}()); 

var jim = new Person("jim", 32); 
var richard = new Person("richard", 27); 
richard.age = 28; 
var alfie = new Person("alfie", 42); 
Person.aveAge() //Returns TypeError: Person.aveAge is not a function 

我已經設置了跨所有實例(研究所)共享一個變量,每一個又一個實例被創建的時間增量,並分配一個唯一的ID。我無法弄清楚如何使用我在底部添加的aveAge原型來獲取存在的所有Person實例的每個「年齡」值。當我試圖調用它來測試變量'inst'保存正確數量的實例時,我也會得到'TypeError:Person.aveAge不是函數'。有人知道我要去哪裏嗎?

+1

首先,'Person.aveAge()'是不是樣機,但在構造函數本身,所以'Person.prototype.aveAge ='應該是'Person.aveAge ='。其次,你使用get/set是不必要的,並且會手動設置你設置的道具。 –

+0

'aveAge()'是Person對象的一個​​方法,比如'jim.aveAge()'。 – jfriend00

+0

也許更像這樣 - > ** http://jsfiddle.net/apgk6mhL/** – adeneo

回答

1

當人們引用人時,讓年齡保持在人身上時感覺很奇怪。請注意,掛在__proto__上的東西使它們可以從構造函數(Person)中獲得,而掛在prototype上的東西可以使它們從實例(richard)中可用。如果更新Age,則需要通過setAge完成,以便PeopleTracker知道更新其內存。另外,在我的例子中,平均值只在需要時計算,而不是每次人們想知道原因。

 var peopleTracker = { 
 
      _count: 0, 
 
      _ages: [], 
 
      averageAge: 0, 
 
      addPerson: function (age) { 
 
       var pt = peopleTracker; 
 

 
       pt._count += 1; 
 
       pt._ages.push(age); 
 
       pt.getAverage(); 
 
      }, 
 
      getAverage: function() { 
 
       var sum = 0, 
 
        pt = peopleTracker; 
 

 
       sum = pt._ages.reduce(function (a, b) { 
 
        return a + b; 
 
       }); 
 

 
       pt.averageAge = Math.round(sum/pt._count); 
 
      }, 
 
      update: function (oldAge, newAge) { 
 
       var pt = peopleTracker, 
 
        ages = pt._ages, 
 
        i = ages.indexOf(oldAge); 
 

 
       ages.splice(i, 1, newAge); 
 
       pt.getAverage(); 
 
      } 
 
     }; 
 

 
     var Person = function (name, age) { 
 
      this.name = name; 
 
      this.age = age; 
 
      peopleTracker.addPerson(age); 
 
     }; 
 

 
     Person.__proto__ = { // available from the constructor 
 
      Constructor: Person, 
 
      setAge: function (age) { 
 
       var oldAge = this.age; 
 
       this.age = age; 
 
       peopleTracker.update(oldAge, age); 
 
      }, 
 
      aveAge: function() { 
 
       return peopleTracker.averageAge; 
 
      } 
 
     }; 
 

 
     Person.prototype = Person.__proto__; // now also available from the instance 
 

 
     var jim = new Person("Jim", 32), 
 
      richard = new Person("Richard", 27), 
 
      alfie = new Person("Alfie", 42); 
 

 
     Person.aveAge(); // 34 
 
     richard.aveAge(); // 34 
 
     richard.setAge(20); 
 
     Person.aveAge(); // 31 
 
     richard.aveAge(); // 31

相關問題