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 ...
随机推荐
- Lucky HDU - 5213 (莫队,容斥)
WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...
- kotlin高阶函数实战&DSL入门
传统函数演示: 这里以电视节目“非诚勿扰”为例,男人去从一大堆美女当中挑选出自己中意的对象,比如台上有24位妹子,其档案如下: 接下来第一个男嘉宾出场啦,如下: 下面用代码来实现一下,比较简单: 先定 ...
- 欧拉函数 || Calculation 2 || HDU 3501
题面: 题解:欧拉函数的基础应用,再套个很 easy 的等差数列前 n 项和就成了. 啊,最近在补作业+准备月考+学数论,题就没怎么写,感觉菜得一匹>_< CSL加油加油~! 代码: #i ...
- 第一次把本地项目与git相连
原文:https://blog.csdn.net/a987625922/article/details/82189863 新建远程仓库(github或者gitee) 将本地仓库转换成版本库,并将文件添 ...
- 关于css阴影和浮动
盒子阴影box-shadow box-shadow:0 0 1px #000 inset; 水平 垂直 模糊 颜色 : [1] inset代表框内阴影,不加inset代表框外阴影 [2]第1个 ...
- 面向对象之封装 及@property装饰器使用
目录 封装 1.封装的定义 2.封装的目的: 3.封装的三种方式 4.封装的优点 5.访问限制(封装) @property 装饰器 属性property底层实现 封装 1.封装的定义 将复杂的丑陋的, ...
- 百度AI训练营笔记
参加了两天百度AI训练营,简单记录一下学到的东西 一.知识图谱 知识图谱是让机器具有积累知识.运用知识的本领. 由于目前知识量很大,所以人工标注的方法无法满足,可以采用数据驱动.自底向上的方式自动构建 ...
- 【51nod 1824】染色游戏
题目 有 n 个红球, m 个蓝球,从中取出 x 个红球和 y 个蓝球排成一排的得分是 rx⋅by ,其中 r0=b0=1 . 定义 f(t) 表示恰好取出 t 个球排成一排的所有可能局面的得分之和. ...
- Java多线程和并发(二),Thread中的start和run的区别
目录 1.调用run方法 2.调用start方法 3.start和run的区别 二.Thread中的start和run的区别 1.调用run方法 public class ThreadTest { p ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 暴力枚举 字符串
A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...