你能用 JavaScript 编写嵌套函数吗? [英] Can you write nested functions in JavaScript?

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

问题描述

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

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!

推荐答案

这真的可能吗.

是的.

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天全站免登陆