简体   繁体   English

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

[英]Calling a Function defined inside another function in Javascript

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.虽然内部函数在外部函数中有一个可用的范围。

You could make it into a module and expose your inner function by returning it in an Object.你可以把它变成一个模块并通过在一个对象中返回它来公开你的内部函数。

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

The scoping is correct as you've noted.正如您所指出的,范围是正确的。 However, you are not calling the inner function anywhere.但是,您不会在任何地方调用inner函数。

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">​

You are not calling the function inner , just defining it.您不是在调用函数inner ,只是定义了它。

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

    inner(); //Call the inner function

}

You can also try this.Here you are returning the function "inside" and invoking with the second set of parenthesis.你也可以试试这个。在这里你返回函数“内部”并用第二组括号调用。

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()();

Again, not a direct answer to the question, but was led here by a web search.同样,不是对问题的直接回答,而是通过网络搜索引导到这里的。 Ended up exposing the inner function without using return, etc. by simply assigning it to a global variable.最终通过简单地将其分配给全局变量来公开内部函数而不使用 return 等。

var fname;

function outer() {
    function inner() {
        console.log("hi");
    }
    fname = inner;
}

Now just现在只是

fname();

If you want to call the "inner" function with the "outer" function, you can do this:如果你想用“外部”函数调用“内部”函数,你可以这样做:

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

And on "onclick" event you call the function like this:在“onclick”事件中,您可以像这样调用函数:

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

you can also just use return:你也可以只使用返回:

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

}
outer();
function outer() { 

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

you should try this你应该试试这个

In JavaScript在 JavaScript 中

If using ES6如果使用 ES6
static functions can be used in a class static函数可以在class

If using ES5如果使用 ES5
After several days of usage, below is what I came up with,经过几天的使用,下面是我想出的,
it is minimal & also has a lot of conveniences:它很小,也有很多便利:

function MathFunctions() {
    let thefo = {}; // the functions object

    thefo.sum = sum = (a, b) => {
        return a + b;
    };
    thefo.avg = avg = (a, b) => {            // name is repeated 2 times - minor inconvenience
        return sum(a, b) / 2;                // calls sum, another function without using 'this'
    };

    return thefo;                            // no need to go down & export here always for each new function - major convenience
}

// Usage
console.log(MathFunctions().sum(1, 2));
console.log(MathFunctions().avg(1, 2));
// OR
const mf = MathFunctions();
console.log(mf.sum(1, 2));
console.log(mf.avg(1, 2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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