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

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

4
  • What error do you get? Where are you calling __construct, do you call it before you initialise the load_todo variable? Commented Mar 6, 2016 at 16:50
  • You probably will want to use function declarations instead of function expressions, and use var for your template and events variables. Commented Mar 6, 2016 at 16:51
  • Is Dashboard object created before var load_todo line ? Commented Mar 6, 2016 at 16:51
  • place your load_todo before your Dashboard. Commented Mar 7, 2016 at 5:13

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.