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. 接口详解例子代码(附Java1.8的接口新特性)

    接口,与抽象类类似但是区别也很大,他们都是标签,用来提醒父类一定要实现的类里创建抽象方法.而接口类可以implements 多个接口,抽象类则只能父类只能继承一个抽象类,与抽象不同的是它不是继承组合关 ...

  2. sql zhuan ORACLE

    DbType   OracleType SqlDatabase OracleDatabase SqlParam.cs SqlServerHelper.cs InsertByHashtableRetur ...

  3. java容器思维导图

    转载自:https://blog.csdn.net/zbdxcyg/article/details/72330833

  4. 学习笔记:jqchart

    (Highcharts 167K:  ECharts 354K: jqChart 240K),如果用于网络,Highchart最小 jqchart 国外的一个图表库 挺漂亮的 http://www.j ...

  5. 44.纯 CSS 创作背景色块变换的按钮特效

    原文地址:https://segmentfault.com/a/1190000015192218 感想: 伪元素作为背景变化. HTML code: <nav> <ul> &l ...

  6. 《算法》第六章部分程序 part 8

    ▶ 书中第六章部分程序,加上自己补充的代码,包括单纯形法求解线性规划问题 ● 单纯形法求解线性规划问题 // 表上作业法,I 为单位阵,y 为对偶变量,z 为目标函数值 // n m 1 // ┌── ...

  7. log-bin

    装mysql,运行一段时间后,在mysql目录下出现一堆类似mysql-bin.000***,从mysql-bin.000001开始一直排列下来,而且占用了大量硬盘空间,高达几十个G. 对于这些超大空 ...

  8. smbpasswd 和 pdbedit 的区别

    以前我们在windows上共享文件的话,只需右击要共享的文件夹然后选择共享相关的选项设置即可.然而如何实现windows和linux的文件共享呢?这就涉及到了samba服务了,这个软件配置起来也不难, ...

  9. 人脸识别68个点<转>

    [Opencv] 于仕琪 人脸68个特征点分布情况 // 鼻尖 30 // 鼻根 27 // 下巴 8 // 左眼外角 36 // 左眼内角 39 // 右眼外角 45 // 右眼内角 42 // 嘴 ...

  10. JSONArray 遍历

    JSONArray 遍历   刚遇到一个接接口任务,发现其中返回数据中,是个字符串数组,数组中就是单个json形式的内容,其实应该也可以称这种数据叫做json数组吧,只不过是字符串形式.而我需要的是将 ...