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. 前端通过js-xlsx获取Excel完整数据

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. [Android] android.util.Log

    android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i()  Log.w() 以及 Log.e() .根据首字母对应VERBOSE,DEBUG,INFO, W ...

  3. SLD Related Gateway Serivces Unavaliable

    SAP NW 7.4 default switched on the ACL (access control list) in gateway service, so only local acces ...

  4. 微信支付开发出现redirect_uri参数错误的解决方法

    我们在进行微信支付开发的时候会遇到出现“redirect_uri参数错误”这种情况,怎么办呢?下面就是我总结出现这种“redirect_uri参数错误”的七种可能情况,以及解决方式. 1.可能原因①: ...

  5. Flex学习笔记-自定义菜单的显示细节

    icon <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx=&qu ...

  6. flash时间轴声音大小控制

    A2时间轴声音大小控制: var sound:Sound = new Sound(); sound.setVolume(200); 把背景音乐放到一个影片剪辑里,剪辑起名 例如bgm_mc 声音模式为 ...

  7. js判断网页是否加载完毕

    1. document.onreadystatechange = function () { if(document.readyState=="complete") { docum ...

  8. spark使用scala读取Avro数据(转)

    这是一篇翻译,原文来自:How to load some Avro data into Spark. 首先,为什么使用 Avro ? 最基本的格式是 CSV ,其廉价并且不需要顶一个一个 schema ...

  9. 使用sigaction函数

    sigaction函数 修改信号处理动作(通常在Linux用其来注册一个信号的捕捉函数) :失败:-1,设置errno 参数: act:传入参数,新的处理方式.oldact:传出参数,旧的处理方式. ...

  10. img标签在div里上下居中

    方法一:图片尺寸未知,IE8-不支持 CSS部分: <style> .content{ width:500px; height:500px; border:1px solid black; ...