简体   繁体   中英

How to call a function from within a function?

how can you put a function in a function?

do_something(){
    alert('do something');
}

also_do_something = function(){
    alert('also do something');
};

inp.onclick = function(){
    do_something();

    // this don't work
    also_do_something;
};

To call a function, you must add the parentheses :

inp.onclick = function(){
    do_something();

    also_do_something();
};

also_do_something is a function reference, you don't call that, you just get it. If you want to call, use also_do_something ()

to call a function, you need to add the parenthesis:

inp.onclick = function(){
    do_something();

    // this don't work
    also_do_something();
};

would be interesting to hear what made you put parenthesis in do_something() and not in also_do_something ?

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