你能用JavaScript编写嵌套函数吗?

时间:2021-02-10 21:22:04

I am wondering if JavaScript supports writing a function within another function, or nested functions (I read it in a blog). Is this really possible?. In fact, I have used these but am unsure of this concept. I am really unclear on this -- please help!

我想知道JavaScript是否支持在另一个函数或嵌套函数中编写函数(我在博客中阅读它)。这真的有可能吗?事实上,我已经使用过这些但不确定这个概念。我真的不清楚 - 请帮忙!

5 个解决方案

#1


151  

Is this really possible.

这真的有可能吗?

Yes.

是。

function a(x) {    // <-- function
  function b(y) { // <-- inner function
    return x + y; // <-- use variables from outer scope
  }
  return b;       // <-- you can even return a function.
}
console.log(a(3)(4));

#2


25  

The following is nasty, but serves to demonstrate how you can treat functions like any other kind of object.

以下是讨厌的,但用于演示如何处理像任何其他类型的对象一样的函数。

var foo = function () { alert('default function'); }

function pickAFunction(a_or_b) {
    var funcs = {
        a: function () {
            alert('a');
        },
        b: function () {
            alert('b');
        }
    };
    foo = funcs[a_or_b];
}

foo();
pickAFunction('a');
foo();
pickAFunction('b');
foo();

#3


11  

Functions are first class objects that can be:

函数是第一类对象,可以是:

  • Defined within your function
  • 在你的功能中定义
  • Created just like any other variable or object at any point in your function
  • 在函数的任何位置创建就像任何其他变量或对象一样
  • Returned from your function (which may seem obvious after the two above, but still)
  • 从您的函数返回(在上面的两个之后可能看起来很明显,但仍然)

To build on the example given by Kenny:

以Kenny给出的例子为基础:

   function a(x) {
      var w = function b(y) {
        return x + y;
      }
      return w;
   };

   var returnedFunction = a(3);
   alert(returnedFunction(2));

Would alert you with 5.

用5提醒你。

#4


11  

Yes, it is possible to write and call a function nested in another function.

是的,可以编写并调用嵌套在另一个函数中的函数。

Try this:

尝试这个:

function A(){
   B(); //call should be B();
   function B(){

   }
}

#5


8  

Not only can you return a function which you have passed into another function as a variable, you can also use it for calculation inside but defining it outside. See this example:

您不仅可以将已传递给另一个函数的函数作为变量返回,还可以将其用于计算内部但在外部定义。看这个例子:

    function calculate(a,b,fn) {
      var c = a * 3 + b + fn(a,b);
      return  c;
    }

    function sum(a,b) {
      return a+b;
    }

    function product(a,b) {
      return a*b;
    }

    document.write(calculate (10,20,sum)); //80
    document.write(calculate (10,20,product)); //250

#1


151  

Is this really possible.

这真的有可能吗?

Yes.

是。

function a(x) {    // <-- function
  function b(y) { // <-- inner function
    return x + y; // <-- use variables from outer scope
  }
  return b;       // <-- you can even return a function.
}
console.log(a(3)(4));

#2


25  

The following is nasty, but serves to demonstrate how you can treat functions like any other kind of object.

以下是讨厌的,但用于演示如何处理像任何其他类型的对象一样的函数。

var foo = function () { alert('default function'); }

function pickAFunction(a_or_b) {
    var funcs = {
        a: function () {
            alert('a');
        },
        b: function () {
            alert('b');
        }
    };
    foo = funcs[a_or_b];
}

foo();
pickAFunction('a');
foo();
pickAFunction('b');
foo();

#3


11  

Functions are first class objects that can be:

函数是第一类对象,可以是:

  • Defined within your function
  • 在你的功能中定义
  • Created just like any other variable or object at any point in your function
  • 在函数的任何位置创建就像任何其他变量或对象一样
  • Returned from your function (which may seem obvious after the two above, but still)
  • 从您的函数返回(在上面的两个之后可能看起来很明显,但仍然)

To build on the example given by Kenny:

以Kenny给出的例子为基础:

   function a(x) {
      var w = function b(y) {
        return x + y;
      }
      return w;
   };

   var returnedFunction = a(3);
   alert(returnedFunction(2));

Would alert you with 5.

用5提醒你。

#4


11  

Yes, it is possible to write and call a function nested in another function.

是的,可以编写并调用嵌套在另一个函数中的函数。

Try this:

尝试这个:

function A(){
   B(); //call should be B();
   function B(){

   }
}

#5


8  

Not only can you return a function which you have passed into another function as a variable, you can also use it for calculation inside but defining it outside. See this example:

您不仅可以将已传递给另一个函数的函数作为变量返回,还可以将其用于计算内部但在外部定义。看这个例子:

    function calculate(a,b,fn) {
      var c = a * 3 + b + fn(a,b);
      return  c;
    }

    function sum(a,b) {
      return a+b;
    }

    function product(a,b) {
      return a*b;
    }

    document.write(calculate (10,20,sum)); //80
    document.write(calculate (10,20,product)); //250