Javascript 如何在另一个函数内部调用一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4524877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:47:09  来源:igfitidea点击:

How do I call a function inside of another function?

javascriptfunction

提问by Web_Designer

I just want to know how to call a javascript function inside another function. If I have the code below, how do I call the second function inside the first?

我只想知道如何在另一个函数中调用 javascript 函数。如果我有下面的代码,我如何在第一个函数中调用第二个函数?

function function_one()
{
alert("The function called 'function_one' has been called.")
//Here I would like to call function_two.
}

function function_two()
{
alert("The function called 'function_two' has been called.")
}

回答by Christian

function function_one() {
    function_two(); // considering the next alert, I figured you wanted to call function_two first
    alert("The function called 'function_one' has been called.");
}

function function_two() {
    alert("The function called 'function_two' has been called.");
}

function_one();

A little bit more context: this works in JavaScript because of a language feature called "variable hoisting" - basically, think of it like variable/function declarations are put at the top of the scope(more info).

多一点上下文:这在 JavaScript 中有效,因为有一种称为“变量提升”的语言功能 - 基本上,可以将其视为变量/函数声明放在作用域的顶部更多信息)。

回答by Luthoz

function function_one() {
  function_two(); 
}

function function_two() {
//enter code here
}

回答by Rajendra Tripathy

function function_one()
{
    alert("The function called 'function_one' has been called.")
    //Here u would like to call function_two.
    function_two(); 
}

function function_two()
{
    alert("The function called 'function_two' has been called.")
}