javascript私有函数调用错误

时间:2016-03-06 16:47:28

标签: javascript function

var Dashboard=function(){   
this.__construct=function(){

    console.log('Dashboard is Created');
    template=new Template();
    events=new Event();
    load_todo();


    //console.log(Template.todo({todo_id:1,content:"This is life"}));
};
//-----------------------------------
var load_todo=function(){
    console.log('load todo is called');
    $.get("api/get_todo",function(o){
        $("#list_todo").html();

        },'json');
};};

我无法调用load_todo()函数,任何人都可以告诉错误是这段代码。语法是错的还是什么?

1 个答案:

答案 0 :(得分:1)

您必须先创建一个对象,然后调用方法:

var Dashboard=function(){   
this.__construct=function(){

    console.log('Dashboard is Created');
    template=new Template();
    events=new Event();
    load_todo();


    //console.log(Template.todo({todo_id:1,content:"This is life"}));
};



//-----------------------------------
var load_todo=function(){
    console.log('load todo is called');
    $.get("api/get_todo",function(o){
        $("#list_todo").html();

        },'json');
};};


//You have to call the construct function
var dashboard = new Dashboard().__construct();

现在,如果您想将您的功能保密,那么您可以执行以下示例:

function Dashboard(){//begin dashboard constructor

    __construct();

function __construct(){

    console.log('Dashboard is Created');
    template=new Template();
    events=new Event();
    load_todo();

    //console.log(Template.todo({todo_id:1,content:"This is life"}));

}


function load_todo(){
    console.log('load todo is called');    

    $.get("api/get_todo",function(o){
        $("#list_todo").html();

    },'json');


}

}//end dashboard constructor


//create a new Dashboard object instance
var dashboard = new Dashboard();

//This will not work because load_todo will be undefined.
console.log(dashboard.load_todo());