Douglas Crockford classified the "class methods" in JavaScript into three types:
private, privileged and public.

Public methods have an obvious meaning: they can be accessed by the public.
Private methods' meaning are also clear: they cannot be accessed by the public.

So what are
privileged methods
? To understand that, we should have a short review of how we implement public and private methods in JavaScript first.

Recall that, unlike languages like C++, JavaScript does not have "class". Moreover, it does not have
access specifiers like public, protected and
private. Lack of these features make the creation of private and public methods less clear than it should be.

To write a
public method
in JavaScript, we make use of the .prototype property of the constructor function. For example:
function FootballPlayer(){}; // declare a function
FootballPlayer.prototype.kick = function(){ alert("kick!"); }; // create a public method kick in .prototype
var Messi = new FootballPlayer();
Messi.kick();

From my previous article, you have learnt that FootballPlayer.prototype is an object that is built automatically when the function is created. Object constructed with the FootballPlayer, Messi, search through his
__proto__ chain when we tell him to kick. Obviously FootballPlayer.prototype is on the chain so Messi knows how to kick. All objects created by the
constructor function share the same FootballPlayer.prototype so they all can invoke the
public method kick!


Private methods are kinda tricky. We declare private variable in a
constructor using the keyword var:
function man() {
var wealth;
var bath = function(){};
function kiss(){}
}

In this case, none of wealth, bath or kiss are accessible outsite the function man. Even man's
public methods cannot access them.


However, the
privileged methods can access the private members due to the existence of
closures. They are very useful as they can act as a bridge between the outsite world and the inner status of the object.

Consider the following example:

var func = function(a,b) {
this.a = a;
this.getB = function(){return b}; // a privileged method
this.setB = function(n){b=n;}; // another privileged method
var b = b;
};
func.prototype.getB2 = function(){return b*2}; // public method var obj = new func(1,2);
alert(obj.getB()); // privileged method can access private b
alert(obj.getB2()); // error: public method cannot access private b
obj.setB(11);
alert(obj.getB());

So actually we can create privileged methods easily by using the keyword
this!



Read More:

Private Members in JavaScript by Douglas Crockford

最新文章

  1. [nRF51822] 15、穿戴式设备上电量检测装置的设计及细节技术点(偏专业硬件文章)
  2. 获取客户端真实ip
  3. 谈EXPORT_SYMBOL使用
  4. mysql 1130 ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server
  5. laravel code bright
  6. ubuntu tengine 安装
  7. 20151203--filter
  8. Material Design学习-----CollapsingToolbarLayout
  9. checkbox 全选或取消
  10. mysql安装了半天参考了文章搞定了
  11. Spring-Cloud-Config学习笔记(一):使用本地存储
  12. [SpringMVC-值传递] 初始SpringMVC--SpringMVC中的值传递
  13. csv文件操作
  14. 第一次java实验报告
  15. css3实现流星坠落效果
  16. VS2010/MFC编程入门之三十八(状态栏的使用详解)
  17. 对 CasperJS 进行远程调试
  18. ZH奶酪:Yii PHP sum SQL查询语句
  19. CentOS6.5中使用 iperf 检测主机间网络带宽
  20. Android——android weight 属性(百度)

热门文章

  1. PHP 安装完成后 增加 bcmath 模块
  2. 专访Vue作者尤雨溪:Vue CLI 3.0重构的原因
  3. Java:获取IP地址
  4. 5.12-leepcode 作业详解
  5. UVa 1354 天平难题 (枚举二叉树)
  6. c++值传递和引用及指针传递区别
  7. ExtJs 滚动条问题
  8. Leetcode 313.超级丑数
  9. poj 1163 数塔
  10. MT6755 使用R63350 IC 出现唤醒概率性闪白,并导致ESD FAIL