Emulating private methods with closures
2017-03-24 10:52
197 查看
【Emulating private methods with closures】
JavaScript does not provide a native way of doing this, but it is possible to emulate private methods using closures. Private methods aren't just useful for restricting access to code: they also provide a powerful way of managing your global namespace, keeping non-essential methods from cluttering up the public interface to your code.
The following code illustrates how to use closures to define public functions that can access private functions and variables.
View Code
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
JavaScript does not provide a native way of doing this, but it is possible to emulate private methods using closures. Private methods aren't just useful for restricting access to code: they also provide a powerful way of managing your global namespace, keeping non-essential methods from cluttering up the public interface to your code.
The following code illustrates how to use closures to define public functions that can access private functions and variables.
var counter = (function() { var privateCounter = 0; function changeBy(val) { privateCounter += val; } return { increment: function() { changeBy(1); }, decrement: function() { changeBy(-1); }, value: function() { return privateCounter; } }; })(); console.log(counter.value()); // logs 0 counter.increment(); counter.increment(); console.log(counter.value()); // logs 2 counter.decrement(); console.log(counter.value()); // logs 1
View Code
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
相关文章推荐
- Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
- [书摘]private properties with closures
- Subscriber class xxx.xxx.xxx and its super classes have no public methods with the@Subscribe annotat
- Duplicate methods named spliterator with the parameters () and () are inherited from the types Colle
- Private virtual networks for Solaris xVM and Zones with Crossbow
- Working with Methods - method_invoke
- Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes
- capsLock key to toggle between input methods [in windows] [with autohotkey]
- Four ways to deal with private members in JavaScript
- Effective Java 03 Enforce the singleton property with a private constructor or an enum type
- Replace Parameter with Explicit Methods (以明确函数取代参数
- Replace Parameter with Methods (以函数取代参数)
- QMetaMethod::invoke: Unable to invoke methods with return values in queued connections
- eclipse使用7.0编译项目报错:Duplicate methods named spliterator with the parameters
- 《reinforcement learning:an introduction》第十一章《Off-policy Methods with Approximation》总结
- Working with hashCode and equals methods in java
- 用闭包优化Python程序 optimize-python-with-closures
- Java Reflection - Private Fields and Methods
- Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP
- Effective Java Item4:Enforce noninstantiability with a private constructor