对象中的数组

    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

 

posted @ 2016-04-09 10:18  绯乐  阅读(486)  评论(0编辑  收藏  举报