在Javascript中调用另一个函数内定义的函数

时间:2022-04-03 23:57:42

I am calling a function on button click like this:

我在按钮点击上调用一个函数,如下所示:

<input type="button" onclick="outer();" value="ACTION">​

function outer() { 
    alert("hi");       
}

It works fine and I get an alert:

它工作正常,我得到一个警报:

Now when I do like this:

现在当我喜欢这样的时候:

function outer() { 
    function inner() {
        alert("hi");
    }
}

Why don't I get an alert?

为什么我没有得到警报?

Though inner function has a scope available in outer function.

虽然内部函数具有外部函数的范围。

4 个解决方案

#1


31  

The scoping is correct as you've noted. However, you are not calling the inner function anywhere.

正如您所指出的那样,范围是正确的。但是,您没有在任何地方调用内部函数。

You can do either:

你可以这样做:

function outer() { 

    // when you define it this way, the inner function will be accessible only from 
    // inside the outer function

    function inner() {
        alert("hi");
    }
    inner(); // call it
}

Or

要么

function outer() { 
    this.inner = function() {
        alert("hi");
    }
}

<input type="button" onclick="(new outer()).inner();" value="ACTION">​

#2


24  

You could make it into a module and expose your inner function by returning it in an Object.

你可以将它变成一个模块,并通过在Object中返回它来公开你的内部函数。

function outer() { 
    function inner() {
        console.log("hi");
    }
    return {
        inner: inner
    };
}
var foo = outer();
foo.inner();

#3


6  

You are not calling the function inner, just defining it.

你没有调用函数inner,只是定义它。

function outer() { 
    function inner() {
        alert("hi");
    }

    inner(); //Call the inner function

}

#4


2  

You can also try this.Here you are returning the function "inside" and invoking with the second set of parenthesis.

你也可以尝试这个。这里你返回函数“inside”并使用第二组括号调用。

function outer() {
  return (function inside(){
    console.log("Inside inside function");
  });
}
outer()();

Or

要么

function outer2() {
    let inside = function inside(){
      console.log("Inside inside");
    };
    return inside;
  }
outer2()();

#1


31  

The scoping is correct as you've noted. However, you are not calling the inner function anywhere.

正如您所指出的那样,范围是正确的。但是,您没有在任何地方调用内部函数。

You can do either:

你可以这样做:

function outer() { 

    // when you define it this way, the inner function will be accessible only from 
    // inside the outer function

    function inner() {
        alert("hi");
    }
    inner(); // call it
}

Or

要么

function outer() { 
    this.inner = function() {
        alert("hi");
    }
}

<input type="button" onclick="(new outer()).inner();" value="ACTION">​

#2


24  

You could make it into a module and expose your inner function by returning it in an Object.

你可以将它变成一个模块,并通过在Object中返回它来公开你的内部函数。

function outer() { 
    function inner() {
        console.log("hi");
    }
    return {
        inner: inner
    };
}
var foo = outer();
foo.inner();

#3


6  

You are not calling the function inner, just defining it.

你没有调用函数inner,只是定义它。

function outer() { 
    function inner() {
        alert("hi");
    }

    inner(); //Call the inner function

}

#4


2  

You can also try this.Here you are returning the function "inside" and invoking with the second set of parenthesis.

你也可以尝试这个。这里你返回函数“inside”并使用第二组括号调用。

function outer() {
  return (function inside(){
    console.log("Inside inside function");
  });
}
outer()();

Or

要么

function outer2() {
    let inside = function inside(){
      console.log("Inside inside");
    };
    return inside;
  }
outer2()();