简体   繁体   中英

How to call a nested function from another function in javascript?

I want to call a inner function from another function.I have code like

$(document).on('click','.td-subject-button',function(){ 
 var current= $(this).parent('td');     
     function setData(){
        return current;
     }
});

function  addSubjectTeacher(){
    var value=setData();
    console.log(value);
}

In the console i am getting the result as ' setData is not defined '. because the setData function is a nested function of a another function. so, how can i invoke setData function.

You can just define your var current outside [globally] and get its value as below.

Assumption - You will be calling addSubjectTeacher function after click event of td-subject-button

var current = "";
$(document).on('click','.td-subject-button',function(){ 
   current= $(this).parent('td');     
});

function  addSubjectTeacher(){
   var value=current;
   console.log(value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM