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. Notes on Noise Contrastive Estimation and Negative Sampling

    Notes on Noise Contrastive Estimation and Negative Sampling ## 生成负样本 在常见的关系抽取应用中,我们经常需要生成负样本来训练一个好的系 ...

  2. 【译】在Flask中使用Celery

    为了在后台运行任务,我们可以使用线程(或者进程). 使用线程(或者进程)的好处是保持处理逻辑简洁.但是,在需要可扩展的生产环境中,我们也可以考虑使用Celery代替线程.   Celery是什么? C ...

  3. val和var和Java

    object Hello { def main(args :Array[String]) { val k = i } } jvm代码 public final class Hello$ { publi ...

  4. SimpleDateFormat线程不安全

    http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html dateUtil替换

  5. 52.纯 CSS 创作一个小球绕着圆环盘旋的动画

    原文地址:https://segmentfault.com/a/1190000015295466 感想:重点在小球绕环转动. HTML code: <div class="contai ...

  6. python学习笔记_week12_mysql

    一.数据库介绍 (一)什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API(接口)用于创建,访问,管理,搜索和复制所保存的数据. ...

  7. 【转】Intro to ShockBurst/Enhanced ShockBurst

    原地址https://devzone.nordicsemi.com/b/blog/posts/intro-to-shockburstenhanced-shockburst Wireless PC ac ...

  8. CentOS 7.0安装配置Vsftp服务器步骤详解

    安装Vsftp讲过最多的就是在centos6.x版本中了,这里小编看到有朋友写了一篇非常不错的CentOS 7.0安装配置Vsftp服务器教程,下面整理分享给各位. 一.配置防火墙,开启FTP服务器需 ...

  9. 08-认识margin

    1.margin margin:外边距的意思.表示边框到最近盒子的距离. /*表示四个方向的外边距离为20px*/ margin: 20px; /*表示盒子向下移动了30px*/ margin-top ...

  10. 2018SDIBT_国庆个人第一场

    A - Turn the Rectangles CodeForces - 1008B There are nn rectangles in a row. You can either turn eac ...