Emulating private methods with closures
【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.
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
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
Emulating private methods with 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
/** * Created with JetBrains PhpStorm. * User: scotty * Date: 28/08/2013 * Time: 19:39 */ ;(function ...
- PowerMock学习(十一)之Mock private methods的使用
Mock private methods 就是mock私有方法啦,学到这不难发现,我们其实大部分都是通过反射去完成单元测试的,但是在实际中,某个类中的私有方法,个人不建议使用反射来测试,因为有时候会 ...
- 【Java基础】Java反射——Private Fields and Methods
Despite the common belief it is actually possible to access private fields and methods of other clas ...
- Private Members in JavaScript
Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...
- How do JavaScript closures work?
Like the old Albert Einstein said: If you can't explain it to a six-year-old, you really don't under ...
- A Simple Example About Privileged Methods in JavaScript
Douglas Crockford classified the "class methods" in JavaScript into three types: private, ...
- CLR via C# 3rd - 08 - Methods
Kinds of methods Constructors Type constructors Overload operators Type con ...
- Do not to test a private method.
If you want to unit test a private method, something may be wrong. Unit tests are (generally speakin ...
- 9.Methods(二)
4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...
随机推荐
- Python中__new__的作用
__new__ 的作用 依照Python官方文档的说法,__new__方法主要是当你继承一些不可变的class时(比如int, str, tuple), 提供给你一个自定义这些类的实例化过程的途径.还 ...
- [Unity动画]06.子状态机
参考链接: https://www.jianshu.com/p/6b1db3d060ac?utm_campaign=maleskine&utm_content=note&utm_med ...
- demo: 全页面CSS3动画的一个参考例子
全页面CSS3动画的一个参考例子: http://wow.blizzard.cn/wow/wod-achievement/ 魔兽的一个活动页 第二页.第三页,文字进入页面 <script src ...
- feedparser的安装
Python中常常要利用RSS下载文本.由于这个Python开源软件嘛,碎片化特别严重.反正是各种边边角角的小问题.网上找来找去找半天都没解决如何安装.我的是win7的.python 是3.4版本的. ...
- 使用Prometheus+Grafana监控MySQL实践
一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...
- WeakReference与SoftReference
WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. WeakReference是弱引用,其中保存的对象实例可以被GC回收掉.这个类通常用于在某 ...
- 尚硅谷redis学习5-初识redis.conf
redis.conf是redis的配置文件,在解压后的redis安装文件夹下 单位 1 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit 2 对大小写不敏感 包含 ...
- Linux命令:ssh-copy-id
ssh-copy-id帮助 SSH-COPY-ID() BSD General Commands Manual SSH-COPY-ID() NAME ssh-copy-id — use locally ...
- Array.Resize(ref arry, size);
数组原来的内容不变,后面添加新的空间. 内部操作应该是:重新分配了一块空间,然后将旧的内容拷过去
- Haskell语言学习笔记(74)GADTs
GADTs GADTs(Generalised Algebraic Data Types,广义代数数据类型)是对代数数据类型的一种扩展. 它允许在定义数据类型时明确指定类型参数的类型并使用模式匹配. ...