Private Variable
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的更多相关文章
- python之private variable
[python之private variable] Since there is a valid use-case for class-private members (namely to avoid ...
- Python私有变量(Private Variable)
Variables can be private which can be useful on many occasions. A private variable can only be chang ...
- Private Variable and Private Method - Python 私有变量 和 私有方法
Private Variable and Private Method Python 不象 Java 那样, 通过 private 关键字创建私有属性, python 通过更简洁的实现了'私有属性', ...
- 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, ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- public、protect、private在父类子类中使用
先贴出一张,直观的.估计大家都见过的关于public.protect.private的范围图 作用域 当前类 同一package 子孙类 其他package public T ...
- java 修饰符的作用一(public protected default private 组)
1.public protected default private 组 public 权限最大,同类,同包,不同包,同包子类父类之间,不同包子类父类之间都可以访问. java 默认的权限是defau ...
- 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 ...
- [转]OpenMP中的private/firstprivate/lastprivate/threadprivate之间的比较
转自:http://blog.csdn.net/gengshenghong/article/details/6985431 private/firstprivate/lastprivate/threa ...
随机推荐
- tomcat9.0 问题汇总
安装时提示 Failed installing tomcat9 service 是因为之前安装tomcat,然后直接删除文件夹,虽然把文件夹删除了,但是重新安装时,服务存在相同的服务名,解决办法:使用 ...
- linux 查找某文件所在路径
find 路径 -name 文件名 例如:find / -name logo_web.png 查找/路径下logo_web.png文件路径 如果为非root账号可用 sudo find / -nam ...
- 深浅拷贝(copy)
目录 copy 模块 1.拷贝(赋值) 1). x为不可变数据类型 2). x为可变数据类型 3). 可变数据类型(比如列表)内,既有不可变元素,又有容器类型可变元素(比如列表) 2.浅拷贝 3.深拷 ...
- MYSQL 大文件导出导入
1.导出sql文件 mysqldump --column-statistics=0 -uusername -ppassword -hyour server ip --default-characte ...
- Linux不同机器文件挂载
由于此前发布项目应用时,需要对两台文件服务器进行文件挂载,所以才实际第一次接触到这个名词,但由于一直以来自己没有真正的去操作过,只是停留在一些理论层次,所以今天记录一下这个实现过程,以备后用. 使用设 ...
- QTCreator:QSS语法高亮(QSS Syntax highlight)
由于QSS几乎等同CSS[1]语法,所以我们设置有 QT 语法高亮: Qtcreator QSS syntax highlight setting: Qt Creator QSS 语法交互设置:QTC ...
- 在window环境下安装numpy模块(包)
刚开始接触python,安装numpy着实费了不少劲.总结一下,新人少走弯路. numpy作用: 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处 ...
- KMP 串的模式匹配 (25 分)
给定两个由英文字母组成的字符串 String 和 Pattern,要求找到 Pattern 在 String 中第一次出现的位置,并将此位置后的 String 的子串输出.如果找不到,则输出“Not ...
- 13.多线程设计模式 - Future模式
多线程设计模式 - Future模式 并发设计模式属于设计优化的一部分,它对于一些常用的多线程结构的总结和抽象.与串行相比并行程序结构通常较为复杂,因此合理的使用并行模式在多线程并发中更具有意义. 1 ...
- 分布式-网络通信-IO-基础(2)
IS 与 OS1. 基本 IO 操作1.1. InputStream 与 OutputStream1.1.1. 输入与输出我们编写的程序除了自身会定义一些数据信息外,经常还会引用外界的数据,或是将自身 ...