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 ...
随机推荐
- [转]IISExpress配置使其能通过IP访问方法
转自http://www.cnblogs.com/liujh/p/4315927.html 环境说明: 本机IP:192.168.2.100:Web端口:11843 步骤一 打开[我的文档]\IISE ...
- 6.简单提取小红书app数据(简单初步试采集与分析)-1
采集小红书数据爬虫:1.本来是要通过app端的接口去直接采集数据,但是app接口手机端设置本地代理这边开启抓包后就不能正常访问数据.所以就采用了微信小程序里的小红书app接口去采集数据. 2.通过 f ...
- 4. easyui删除功能实例
############实例1############## function delPerson(id) { $.messager.defaults = { ok : "确定", ...
- View Stack容器,按钮选择子容器
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- JAVA SpringBoot2 整合 JSP视图模板 整合 Ueditor富文本编辑器
一般涉及到后台管理系统,就少不了富文本编辑器,这个可以图,文,视频混排的高级工具,笔者通过对比,发现目前市场上最好的三方库还当属百度的 ueditor 近年来 SpringBoot 框架可谓越来越火, ...
- HTML5 Canvas 小例子 旋转的时钟
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- poi excel 合并单元格
结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip, colId, colId + c ...
- react-navigation设置navigationOptions中Static中使用 this 的方法
使用react-navigation时,单页面设置navigationOptions中,进行Static中 调用this 中的方法或值时,需要做如下操作 static navigationOption ...
- 浅谈transient关键字
1,用途 当一个对象实现了Serilizable接口,这个对象就可以被序列化.而有时候我们可能要求:当对象被序列化时(写入字节序列到目标文件)时,有些属性需要序列化,而其他属性不需要被序列化,打个比方 ...
- screen 命令安装使用
初次接触Linux的朋友总会有个感觉:Windows平台想同时运行多个操作,执行多个程序或命令只需要打开程序即可:但在Linux中,命令行就一个,要想同时执行多个命令如何操作? 其实,只需要一个简简单 ...