Zulfiqar Tariq
Zulfiqar Tariq

Reputation: 394

javascript private function call error

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');
};};

I am unable to call the load_todo() function, can anyone tell the error is this code. is the syntax wrong or what?

Upvotes: 1

Views: 104

Answers (1)

Larry Lane
Larry Lane

Reputation: 2191

You will have to create an object first and then call the method:

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();

Now if you want to keep your functions private then you can do something like the following example:

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());

Upvotes: 1

Related Questions