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 ...
随机推荐
- 关于QT_Creator不能在线调试问题
电脑:W7+64位,QT:5_7_0(vs2015版本) 用QTcreator进行在线调试时出现找不到“engine...”,原因是没有在线调试软件 CDB下载地址:http://msdn.micro ...
- mysql-5.5.50-winx64
1.help 2.Service 3.Configure 4.User 5.design last 1.获取帮助文档 cd C:\Program Files\mysql\mysql-5.5.50-wi ...
- LeetCode 12. Integer to RomanLeetCode
整数转罗马数字 first submission import math class Solution: def __init__(self): self.roman={1:'I',5:'V',10: ...
- python3封装Api接口
注:本篇的代码和语法基于Python3.5环境,下面将用到Python 的Flask框架 封装接口主要讲静态接口(无参数传入).动态接口(有参数传入,不同参数返回的信息不同).针对动态接口有三种传参方 ...
- 432 4.3.2 STOREDRV.Deliver; recipient thread limit exceeded
最近几天Hub-Mailbox服务器时不时就CPU超过90%.在任务管理器里面看到edgetransport占用大量CPU.进入EMC的队列查看器,看到邮箱数据库堵塞,队列上万. 堵塞的邮件大多是收件 ...
- 4. powerdesigner 生成sql脚本步骤
1. 选择数据库类型:DataBase(数据库)-- Change Current DBMS 2. 生成数据库脚本:DataBase(数据库)--generate Database
- Android内存泄漏原因
这段时间调试APP的时候,发现程序在加载了过多的bitmap后会崩溃.查看了日志,原来是发生了内存溢出(OOM).第一次遇到这样的问题,那就慢慢排查吧. 内存优化可以参考胡凯大神的博客Android内 ...
- windows系统安装
系统最新地址:https://www.microsoft.com/zh-cn/software-download/windows10
- redis内部数据结构和外部数据结构揭秘
Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 很多人面试时都遇到过这种场景吧? 其实除了上面的几种常见数据结构,还需要加上数据结 ...
- react-native-echarts 安卓版打包后,部分手机图表不显示问题
1. 找到 node_modules\native-echarts\src\components\Echarts\tpl.html 文件 ,把它复制到 (android\app\src\main\a ...