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的一些收集

    判断jquery文件有没有加载 !window.jQuery && alert('jQuery未导入!请确认路径是否正确'); 禁止页面跳转 javascript:void(0); / ...

  2. 导航栏 ------ z-index

    z-index 显示的层叠关系,数字越大越在上面 <!DOCTYPE html> <html lang="en"> <head> <met ...

  3. mac python3安装virtualenv出现的问题

    pip3 install virtualenv pip3 install virtualenvwrapper 安装成功后可能 找不到该命令, 解决办法 1.在 vim ~/.bashrc export ...

  4. 阿里Canal配置(编写中)

    首先在源mysql的.ini文件中进行配置 [client]default-character-set=utf8 [mysqld]basedir = E:/testCanal/mysql-5.7.17 ...

  5. 输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。程序设计思路。

    将文件内容读取后存入StringBuffer中. 利用函数将段落分割成字符串,按(“,”,“.”,“!”,“空格”,“回车”)分割,然后存入数组中. 遍历数组,并统计每个单词及其出现的次数. 要求出文 ...

  6. mongodb对数据库的基本操作

    数据库切换 查看当前数据库名称 db 查看所有数据库名称 列出所有在物理上存在的数据库 show dbs 切换数据库 如果数据库不存在,则指向数据库,但不创建,直到插入数据或创建集合时数据库才被创建 ...

  7. 《汇编语言 基于x86处理器》第十章 - 运行一个 16位实地址汇编程序

    ▶ 书上第 10 章,主要讲了宏,引用了一个 16 位实地址的程序,从代码开始到运行 ● 代码 ; main.asm INCLUDE Macros.inc IF IsDefined( RealMode ...

  8. hive-client heap内存大小的配置优先级

    hive-client Heap大小的配置优先级 其实主要解决,hive作为数据仓库(hive -e "select ····") 如果是分区表且分区较多可能导致hive 堆内存溢 ...

  9. OneinStack PHP多版本共存教程

    1. 安装OneinStack 选择lnmp模式,默认安装php5.4,安装步骤参考:http://oneinstack.com/install/ 选项如下: Install Nginx Do not ...

  10. zookeeper 入门知识

    作为开启分布式架构的基石,除了必会还有的选么 自己的一些理解,有错误的话请一定要给予指正! 一.是什么? 分布式数据一致性的解决方案. 二.有什么用 数据的发布/订阅(配置中心)  . 负载均衡(du ...