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

A Simple Example About Privileged Methods in JavaScript的更多相关文章

  1. Static and Instance Methods in JavaScript

    class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...

  2. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  3. Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数

    原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...

  4. Classical Inheritance in JavaScript

    JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...

  5. Private Members in JavaScript

    Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...

  6. [转]Running JavaScript in an iOS application with JavaScriptCore

    原文:https://www.infinum.co/the-capsized-eight/articles/running-javascript-in-an-ios-application-with- ...

  7. 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 ...

  8. JavaScript中Object的总结

    基于原型继承,动态对象扩展,闭包,JavaScript已经成为当今世界上最灵活和富有表现力的编程语言之一. 这里有一个很重要的概念需要特别指出:在JavaScript中,包括所有的函数,数组,键值对和 ...

  9. 在JavaScript中使用json.js:Ajax项目之POST请求(异步)

    经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...

随机推荐

  1. LeetCode728. 自除数

    自除数 是指可以被它包含的每一位数除尽的数. 例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0. 还有,自除数不允许包含 0 . 给定上边 ...

  2. python中的函数的分类

    函数的种类 传参的基本要求 默认参数 *args 关键字参数 **kwargs 普通函数 带参数 默认参数 def text(a,b=2) print("haha") print( ...

  3. LeetCode(15) 3Sum

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  4. 【HIHOCODER 1049】 后序遍历

    描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思的玩具--一棵由小球和木棍连接起来的二叉树! 小Ho对这棵二叉树爱不释手,于是给它的每一个节 ...

  5. pycharm运行没问题,但是在命令行执行就报错

    因为python并不知道你那个叫demo的package在哪里.你需要手动把project的完整路径添加到PYTHONPATH这个环境变量中.pycharm执行项目中的文件时会自动帮你做这件事,所以你 ...

  6. 关联及web_reg_save_param

    一.什么是关联 关联(correlation):脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服务器所响应的内容中查 找,得到相应的值,已变量的形式替换录制时的静 ...

  7. Relocation(状压DP)

    Description Emma and Eric are moving to their new house they bought after returning from their honey ...

  8. 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...

  9. 博弈 Nim问题 POJ2234

    定义: 通常的Nim游戏的定义是这样的:有若干堆石子,每堆石子的数量都是有限的,合法的移动是 “选择一堆石子并拿走若干颗(不能不拿)”,如果轮到某个人时所有的石子堆都已经被拿空了, 则判负(因为他此刻 ...

  10. Sencha Touch 2 实现跨域访问

    最近要做手机移动App,最后采用HTMML5来做框架用Sencha Touch,在数据交互时遇到了Ajax跨域访问,在Sencha Touch 2中,实现跨域访问可以使用Ext类库提供给我们的类Ext ...