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. java8学习之内部迭代与外部迭代本质剖析及流本源分析

    关于Stream在Java8中是占非常主要的地位的,所以这次对它进行进一步探讨[这次基本上都是偏理论的东东,但是理解它很重要~],其实流跟咱们数据库学习当中的sql语句的特点是非常非常之像的,为什么这 ...

  2. SpringBoot 出现内置的Tomcat没有启动的情况

    Tomcat未启动,也未报错 pom.xml文件中增加 <dependency> <groupId>org.springframework.cloud</groupId& ...

  3. 实践:Linux用户、组和密码相关文件被破坏如何恢复系统

    我们先看一下用户用户组和密码相关文件: 1 2 3 4 5 6 7 8 9 [root❄centos7 ~]☭ ll /etc/passwd* /etc/shadow* /etc/group* /et ...

  4. keep-alive实现返回保留筛选条件及筛选结果

    实现页面返回时,保留筛选条件和筛选结果 说明 . keep-alive 是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM 实现 结合router实现部分页面缓存 模板应用 ...

  5. C语言 - strcat和strncat的编程实现及总结

    一.函数strcat与stcncat的函数实现 1.strcat函数的实现 要求: 原型:char * strcat(char *dest, const char *src);    头文件:#inc ...

  6. G.subsequence 1(dp + 排列组合)

    subsequence 1 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 You are ...

  7. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

  8. C++入门经典-例3.17-使用while循环进行计算

    1:代码如下: // 3.17.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...

  9. MqttConnectReturnCode枚举值

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  10. TCP定时器 之 FIN_WAIT_2定时器

    当TCP主动关闭一端调用了close()来执行连接的完全关闭时会执行以下流程,本端发送FIN给对端,对端回复ACK,本端进入FIN_WAIT_2状态,此时只有对端发送了FIN,本端才会进入TIME_W ...