var Food={
    fruits: [
        { name: "Banana", color: "Yellow" },
        { name: "Apple", color: "Red" },
        { name: "Grapefruit", color: "Orange" },
        { name: "Kiwi", color: "Green" }
        ],
    vegetables: [
        { name: "Carrot", color: "Orange" },
        { name: "Turnip", color: "Purple" },
        { name: "Rutabaga", color: "Yellow" },
        { name: "Sweet Potato", color: "Orange" }
        ],
    luger: [
     {name:"luger",color:"red"},
     {name:"lee",color:"pink"}
     ]
    };///////////////////下面的代码是想设计成上面的样式,大家看看设计上有问题吗?///////////////////
var toDoList = [];  // 加了这个定义,你忘了定义它
var Luger=(function(){ // 这里用了闭包
/**
 * ToDo 对象
 */
function ToDo(day,title,content,time,rank,color) {
this.day = day;
        this.title = title;
        this.content = content;
        this.time = time;
        this.rank = rank;
        this.color = color;
}

    return { // API 函数定义返回给 Luger
        newToDo:function(day,title,content,time,rank,color){
            return new ToDo(day,title,content,time,rank,color);
        },
        addToDo:function(toDo){
         if(typeof(toDoList[toDo.day])=="undefined"){
         toDoList[toDo.day] = [toDo];
         } else {
         toDoList[toDo.day].push(toDo);
         }
            
        },
        newAndAddToDo:function(day,title,content,time,rank,color){
            var toDo = this.newToDo(day,title,content,time,rank,color);
            this.addToDo(toDo);
        }
    }})();
$(document).ready(function(){
//测试代码示例:
var todo = Luger.newToDo("2013-04-02","title1","content1",new Date(),1,"red");
Luger.addToDo(todo);
var todo = Luger.newToDo("2013-04-02","title2","content2",new Date(),2,"blue");
Luger.addToDo(todo);
Luger.newAndAddToDo("2013-04-02","title3","content3",new Date(),3,"blue");
alert(toDoList["2013-04-02"].length);
});jsonjavascript

解决方案 »

  1.   

    var Food={
        fruits: [
            { name: "Banana", color: "Yellow" },
            { name: "Apple", color: "Red" },
            { name: "Grapefruit", color: "Orange" },
            { name: "Kiwi", color: "Green" }
            ],
        vegetables: [
            { name: "Carrot", color: "Orange" },
            { name: "Turnip", color: "Purple" },
            { name: "Rutabaga", color: "Yellow" },
            { name: "Sweet Potato", color: "Orange" }
            ],
        luger: [
            {name:"luger",color:"red"},
            {name:"lee",color:"pink"}
            ]
        };
     
    ///////////////////下面的代码是想设计成上面的样式,大家看看设计上有问题吗?///////////////////
    var toDoList = {};  // 加了这个定义,你忘了定义它。这里改成这个,不然是个空数组,又给这个空数组强加了2013-04-02的属性
    var Luger=(function(){ // 这里用了闭包
        /**
         * ToDo 对象
         */
        function ToDo(day,title,content,time,rank,color) {
            this.day = day;
            this.title = title;
            this.content = content;
            this.time = time;
            this.rank = rank;
            this.color = color;
        }
         
        return { // API 函数定义返回给 Luger
            newToDo:function(day,title,content,time,rank,color){
                return new ToDo(day,title,content,time,rank,color);
            },
            addToDo:function(toDo){
                if(typeof(toDoList[toDo.day])=="undefined"){
                    toDoList[toDo.day] = [toDo];
                } else {
                    toDoList[toDo.day].push(toDo);
                }
                 
            },
            newAndAddToDo:function(day,title,content,time,rank,color){
                var toDo = this.newToDo(day,title,content,time,rank,color);
                this.addToDo(toDo);
            }
        }
     
    })();
     
     
    $(document).ready(function(){
        //测试代码示例:
    var todo = Luger.newToDo("2013-04-02","title1","content1",new Date(),1,"red");
    Luger.addToDo(todo);
    var todo = Luger.newToDo("2013-04-02","title2","content2",new Date(),2,"blue");
    Luger.addToDo(todo);
    Luger.newAndAddToDo("2013-04-02","title3","content3",new Date(),3,"blue");
    alert(toDoList["2013-04-02"].length);
    });