I've used closures to do things like:(我用闭包来做类似的事情:)
a = (function () {
var privatefunction = function () {
alert('hello');
}
return {
publicfunction : function () {
privatefunction();
}
}
})();
As you can see there, a
is now an object, with a method publicfunction
( a.publicfunction()
) which calls privatefunction
, which only exists inside the closure.(如您所见, a
现在是一个对象,它具有方法publicfunction
( a.publicfunction()
),该方法调用privatefunction
,该方法仅存在于闭包内部。)
You can NOT call privatefunction
directly (ie a.privatefunction()
), just publicfunction()
.(你不能叫privatefunction
直接(即a.privatefunction()
只是publicfunction()
)
Its a minimal example but maybe you can see uses to it?(它是一个最小的示例,但也许您可以看到它的用处?)
We used this to enforce public/private methods.(我们使用它来强制执行公共/私有方法。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…