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. Ubuntu安装opencv 为调用gpu模块

    也真够折腾的. 事件背景:为了一个光流的提取处理,本来是3.1的opencv在include一些模块上出错,原因是opencv3.0以上的版本对模块进行了再分离,要contribute,但是contr ...

  2. Python的语句---钱中平

    python中的两种语句:1.if条件控制语句 格式: if: a = int(input("请输入第一个数:"))b = int(input("请输入第二个数:&quo ...

  3. 后端返回图片的url,将其转成base64,再次进行上传

      //将图片变成base64再上传(主要是转化来自客户端的图片)  getUrlBase64=(url, ext)=> {     var canvas = document.createEl ...

  4. count(列) count(*)

    总结:但是真的结论是这样的么.其实不然.其实在数据库中count(*)和count(列)根本就是不等价的,count(*)是针对于全表的,而count(列)是针对于某一列的,如果此列值为空的话,cou ...

  5. QT Creator 添加Lib库

    LIBS += -LD:/Path/ -lTest1.lib -lTest2.lib 解析:在路径D:/Path/ 下 添加Test1.lib Test2.lib库 -L 和小写'-l"没有 ...

  6. Editplus注册码生成代码

    function generate_editplus_regcode(username) { var list = [0,49345,49537,320,49921,960,640,49729,506 ...

  7. web前端:上传文件夹(需支持多浏览器)

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  8. 2018 焦作网络赛 L Poor God Water ( AC自动机构造矩阵、BM求线性递推、手动构造矩阵、矩阵快速幂 )

    题目链接 题意 : 实际上可以转化一下题意 要求求出用三个不同元素的字符集例如 { 'A' .'B' .'C' } 构造出长度为 n 且不包含 AAA.BBB CCC.ACB BCA.CAC CBC ...

  9. 报错:没有与参数列表匹配的构造函数 "CFileDialog::CFileDialog" 实例

    如果是在解决方案管理器窗口内,右击你的项目“项目”,然后选“属性”(最后一项),再点“配置属性”,是个“+”号,把它展开,然后选“常规”选项卡,倒数第三项“字符集”,选择“使用多字节字符集”.再编译应 ...

  10. POJ 3061  Subsequence   尺取法   挑战146页

    ---恢复内容开始--- Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10487   Accept ...