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的更多相关文章

  1. 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 ...

  2. PowerMock学习(十一)之Mock private methods的使用

    Mock  private methods 就是mock私有方法啦,学到这不难发现,我们其实大部分都是通过反射去完成单元测试的,但是在实际中,某个类中的私有方法,个人不建议使用反射来测试,因为有时候会 ...

  3. 【Java基础】Java反射——Private Fields and Methods

    Despite the common belief it is actually possible to access private fields and methods of other clas ...

  4. Private Members in JavaScript

    Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...

  5. 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 ...

  6. A Simple Example About Privileged Methods in JavaScript

    Douglas Crockford classified the "class methods" in JavaScript into three types: private, ...

  7. CLR via C# 3rd - 08 - Methods

       Kinds of methods        Constructors      Type constructors      Overload operators      Type con ...

  8. 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. 9.Methods(二)

    4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...

随机推荐

  1. Node 在 Centos7 系统下的安装

    1,下载二进制包  https://nodejs.org/zh-cn/download/     根据自己的需求选择对应的版本,不推荐使用源码包,容易出现错误 2,上传到 linux 服务器 3, 解 ...

  2. Vue.js——60分钟组件快速入门(下篇)

    转自:https://www.cnblogs.com/keepfool/p/5637834.html 概述 上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子 ...

  3. 学习opengl第一步

    有两个地址一个是学习opengl基础知识的网站, 一个是博客园大牛分享的特别好的文章. 记录一下希望向坚持做俯卧撑一样坚持下去. 学习网站:http://learnopengl-cn.readthed ...

  4. UI5-学习篇-4-SCP-SAP WEB IDE登录

    1.注册SAP账号 登录SAP官网:https://www.sap.com/index.html 注册Register 填完相关信息,勾选条款,然后提交. 账号激活:完成后需到Email邮件中激活链接 ...

  5. LeetCode 题解 Permutation Sequence 需要优化!

    题目大意:给出n和k,找到1..n这些数组成的有序全排列中的第k个. 首先,n的全排列可以分成n组,每一组由n-1个数组成. 例如  3的全排列,分成三组: 1 2 3  和 1 3 2 2 1 3 ...

  6. centos7,进程最大打开文件数 too many open files错误

    遇到一问题,tomcat最近发生几次异常,查看日志,发现一直报 too many open files,熟悉的同学都知道这是用户打开文件数过多导致的, 再用命令ls /proc/20861/fd/ | ...

  7. Unable to connect to zookeeper server within timeout: 5000

    错误 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error crea ...

  8. VC中BSTR、Char和CString类型的转换

    1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...

  9. java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor

    Hibernate4.x与spring3.x整合,有关事务的处理,用Junit4测试,出现org.springframework.beans.factory.BeanCreationException ...

  10. E_FAIL (0x80004005) MachineWrap

    下载VirtualBox-4.3.12-93733-Win.exe,下载地址:http://download.virtualbox.org/virtualbox/4.3.12/