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 ...
随机推荐
- 标准C语言(12)
一个存储区的地址应该是它自身大小的整数倍(双精度浮点类型存储区的地址只需要是4的整数倍),这个规则叫数据对齐,结构体内部的存储区通常也需要遵守数据对齐的规则,数据对齐有可能导致结构体相邻子存储区之间有 ...
- 牛客练习赛46 C 华华跟奕奕玩游戏 (期望,概率)(详解)
链接:https://ac.nowcoder.com/acm/contest/894/C 来源:牛客网 华华跟奕奕玩游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...
- 也来谈谈SQL SERVER 自定义函数~
在使用SQL SERVER 数据库的时候,函数大家都应该用过,简单的比如 系统聚合函数 Sum(),Max() 等等.但是一些初学者编写自定义函数的时候,经常问起什么是表值函数,什么是标量值函数. 表 ...
- 细数meta标签的奥秘
因为看到了一个很不错的h5自适应网站,觉得很不错,于是好奇心作祟,让我翻开了它的源码一探究竟,最先研究的是它的meta标签,好了,废话不多说,以下是我总结的和比较实用的meta标签,如有错误,请多多指 ...
- HTTP缓存剖析
web浏览器会自动缓存访问过的页面,当访问同一个页面的请求时,浏览器不再从服务器中重新下载页面而是优先使用本地缓存中的页面 为什么要进行web缓存 从用户的角度来看web缓存加快了上网速度,当然这是用 ...
- DevExpress ASP.NET Core v19.1版本亮点:数据网格和树列表
行业领先的.NET界面控件DevExpress 发布了v19.1版本,本文将以系列文章的方式为大家介绍DevExpress ASP.NET Core Controls v19.1中新增的一些控件及增强 ...
- 线程异步更新UI
winform程序一般是不允许非主线程操作ui,单可以通过线程与委托的方式并结合Control类提供的BeginInvoke机制进行ui更改 如下,这是更新ui的方法 private void upU ...
- buuctf@[OGeek2019]babyrop
#!/usr/bin/python #coding:utf-8 from pwn import * #context.log_level = 'debug' io = process('./pwn', ...
- AHOI/HNOI2017 礼物
题目链接:戳我 对于题目中给的式子:(大家暂且把\(y_i\)当作\(y_{i+k}\)来看啦qwq) \(\sum_{i=1}^{n}(x_i-(y_i+c))^2\) \(=\sum_{i=1}^ ...
- Makefile样例
Makefile1 src = $(wildcard ./*cpp) obj = $(patsubst %.cpp, %.o,$(src)) target = test $(target) : $(o ...