开发者

Conditional creation of closures in JavaScript

开发者 https://www.devze.com 2023-03-30 20:23 出处:网络
Say I have a function like this: f开发者_如何学编程unction wrap_function(fnInput) { if (somecondition)

Say I have a function like this:

f开发者_如何学编程unction wrap_function(fnInput)
{
    if (somecondition)
    {
        return function() { 

            // Simplified example, in reality doing more stuff in here
            fnInput.apply(this, arguments)

        }
    }
    else
    {
        return fnInput;
    }
}

I'm assuming that if somecondition is false this function won't create a closure and therefore won't have performance/memory impacts associated with closures.

Is this correct?


If somecondition is false a closure will not be created. You're using the loosely typed nature of javascript in your example

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号