你可以在JavaScript中编写嵌套函数吗? [英] Can you write nested functions in JavaScript?

查看:101
本文介绍了你可以在JavaScript中编写嵌套函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案


这是否真的有可能。

p>

是。

  function a(x){//<  - 函数函数b(y){//<  - 内函数返回x + y; //<  - 使用来自外部范围的变量} return b; //<  - 你甚至可以返回一个函数。} console.log(a(3)(4));  


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!

解决方案

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

这篇关于你可以在JavaScript中编写嵌套函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆