将函数相互嵌套是不好的做法吗? [英] Is it bad practice to nest functions within each other?

查看:32
本文介绍了将函数相互嵌套是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌套函数内嵌套函数内嵌套函数有什么缺点...

What are the disadvantages to nesting a function within a nested function within a nested function...

这是一个例子:

JS/jQuery:

function one() {

    // do something

    function two() {

        // do something

        function three() {

            // do something

            function four() {

                // do something

            }

        }

    }
}

推荐答案

声明嵌套函数的一个缺点是每次调用父函数时它都会在函数的环境中创建.

One disadvantage of declaring a nested function is the fact that it will be created inside function's environment every time you call the parent function.

理论上,如果频繁调用父函数,这可能会降低性能.

In theory, this could decrease performance if the parent function is called frequently.

但是,嵌套函数在 Javascript 中使用非常多.例如,闭包非常强大,所有 JavaScript 开发人员都应该理解.

But, nested functions are very much used in Javascript. For example, closures are extremely powerful and should be understood by all JavaScript developers.

更多关于闭包的信息

这篇关于将函数相互嵌套是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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