function weekTemps() { this.dataStore = [];//属性 this.add = add;//方法 this.average = average;//方法 } function add(temp) { this.dataStore.push(temp); } function average() { var total = 0; for ( var i = 0; i < this.dataStore.length; ++i) { total += this.dataStore[i]; } return total / this.dataStore.length; } var thisWeek = new weekTemps(); thisWeek.add(52); thisWeek.add(55); thisWeek.add(61); thisWeek.add(65); thisWeek.add(55); thisWeek.add(50); thisWeek.add(52); thisWeek.add(49); alert(thisWeek.average()); // 显示 54.875