Any variable defined inside a function is considered private since it is inaccessable outside that function. This includes function arguments, local variables, and functions defined inside other functions.

  A privileged method is a public method that has access to private variables and/or private functions. There are two ways to create privileged methods on objects. The first is to do so inside a constructor, as in this example:

 function MyObject(){
var privateVariable = ; this.publicMethod = function(){
privateVariable++;
console.log(privateVariable);
};
} var object1 = new MyObject();
object1.publicMethod(); var object2 = new MyObject();
object2.publicMethod();

  One downside, however, is that you must use the constructor pattern to accomplish this result. As discussed in Chapter 6, the constructor pattern is flawed in that new methods are created for each instance. Using static private variables to achieve privileged methods avoids this problem.

Static Private Variables

  Privileged methods can also be created by using a private scope to define the private variables or functions. The pattern is as follows:

 (function(){
var privateVariable = 10;
MyObject = function(){};
MyObject.prototype.publicMethod = function(){
privateVariable++;
console.log(privateVariable);
} ;
})(); var object1 = new MyObject();
object1.publicMethod(); var object2 = new MyObject();
object2.publicMethod();

  The main difference between this pattern and the pervious one is that private variables and functions are shared among instances.

  Creating static private variables in this way allows for better code reuse through prototypes, although each instance doesn't have its own private variable. Ultimately, the decision to use instance or static private variables needs to be based on your individual requirements.

The Module Pattern

  The module pattern, as described by Douglas Crockford, does the same for singletons. Singletons are objects of which there will only ever be one instance. Traditionally, singletons are created in JavaScript using object literal notation.

  The module pattern augments the basic singleton to allow for private variables and privileged methods, taking the following format:

 var singleton = function(){
var privateVariable = 10;
function privateFunction(){
return false;
} return {
publicProperty: true,
publicMethod: function(){
privateVariable++;
return privateFunction();
}
};
}(); console.log(singleton.publicProperty);
console.log(singleton.publicMethod());

  The module pattern is useful for cases like this, when a single object must be created and initialized with some data and expose public methods that have access to private data.

The Module-Augmentation Pattern

  Another take on the module pattern calls for the augmentation of the object before returning it. This pattern is useful when singleton object needs to be an instance of a particular type but must be augmented with additional properties and/or methods. Consider the following example:

 var singleton = function(){

   // private variables and functions
var privateVariable = 10;
function privateFunction(){
return false;
} // create object
var object = new CustomType(); // add privileged/public properties and methods
object.publicProperty = true;
object.publicMethod = function(){
privateVariable++;
return privateFunction();
}; // return the object
return object;
}();

Private Variable的更多相关文章

  1. python之private variable

    [python之private variable] Since there is a valid use-case for class-private members (namely to avoid ...

  2. Python私有变量(Private Variable)

    Variables can be private which can be useful on many occasions. A private variable can only be chang ...

  3. Private Variable and Private Method - Python 私有变量 和 私有方法

    Private Variable and Private Method Python 不象 Java 那样, 通过 private 关键字创建私有属性, python 通过更简洁的实现了'私有属性', ...

  4. To add private variable to this Javascript literal object

    You can use number as function/variable name, the numberic name can't be accessed from parent scope, ...

  5. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  6. public、protect、private在父类子类中使用

    先贴出一张,直观的.估计大家都见过的关于public.protect.private的范围图 作用域 当前类 同一package 子孙类 其他package public     T         ...

  7. java 修饰符的作用一(public protected default private 组)

    1.public protected default private 组 public 权限最大,同类,同包,不同包,同包子类父类之间,不同包子类父类之间都可以访问. java 默认的权限是defau ...

  8. OOP in JS Public/Private Variables and Methods

    Summary private variables are declared with the 'var' keyword inside the object, and can only be acc ...

  9. [转]OpenMP中的private/firstprivate/lastprivate/threadprivate之间的比较

    转自:http://blog.csdn.net/gengshenghong/article/details/6985431 private/firstprivate/lastprivate/threa ...

随机推荐

  1. jmeter通过if控制器控制业务比例

    以发帖,看帖,回帖三个事物为例,这里就10个用户跑10次,进行测试下: 可以看到看帖,回帖,发帖比例是5:3:2,先来说说怎么做到的,就是通过if控制器,分别来看下几个控制器的内容, 看帖(if控制器 ...

  2. 【LeetCode】451-根据字符出现频率排序

    题目描述 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和' ...

  3. Android九宫格解锁有多少种姿势

    参考知乎:知乎.

  4. OCP协议_电信特殊协议

    OCP(Online Charging Protocol)协议——在线计费协议(也称为AAA协议),是中国电信(文中以中国电信为主)充分研究国内外在线计费协议,基于中国电信自己在线计费的需求,参考3G ...

  5. Nginx 的简介

    1. 什么是 nginx :Nginx 是高性能的 HTTP 和反向代理的服务器,处理高并发能力是十分强大的,能经受高负 载的考验,有报告表明能支持高达 50,000 个并发连接数.  2. 正向代理 ...

  6. 并发编程的锁机制:synchronized和lock

    1. 锁的种类 锁的种类有很多,包括:自旋锁.自旋锁的其他种类.阻塞锁.可重入锁.读写锁.互斥锁.悲观锁.乐观锁.公平锁.可重入锁等等,其余就不列出了.我们重点看如下几种:可重入锁.读写锁.可中断锁. ...

  7. XXL-JOB原理--任务调度中心任务管理

    XXL-JOB原理--任务调度中心任务管理 https://blog.csdn.net/qq924862077/article/details/82713758

  8. JAVA基础编程之打印99乘法表

    需求:打印9*9乘法表 技术考核: 1.for嵌套循环 代码: // 打印99乘法表 public static void print99Table() { System.out.println(&q ...

  9. Linux设备驱动学习笔记

    之前研究Linux设备驱动时做的零零散散的笔记,整理出来,方便以后复习. 1.1驱动程序的的角色 提供机制 例如:unix图形界面分为X服务器和窗口会话管理器 X服务器理解硬件及提供统一的接口给用户程 ...

  10. C#使用BinaryReader类读取二进制文件

    Close():关闭BinaryReader对象:   Read():从指定流读取数据,并将指针迁移,指向下一个字符.   ReadDecimal():从指定流读取一个十进制数值,并将在流中的位置向前 ...