A Simple Example About Privileged Methods in JavaScript

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:

Prototype and Constructor in JavaScript by RedcapCoder

Private Members in JavaScript by Douglas Crockford

时间: 2024-11-07 12:05:34

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

JavaScript Patterns 5.3 Private Properties and Methods

All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return this.myprop; } }; console.log(myobj.myprop); // `myprop` is publicly accessible console.log(myobj.getProp()); // getProp() is public too The same is

JavaScript Good Parts学习笔记-数组篇

数组是一段线性分配的内存.通过计算偏移来访问其中的元素. 不幸的是,JavaScript并没有像数组一样的数据结构 作为替代,JavaScript提供了类数组的对象,把数组下标改为字符串. 1 数组字面量(Array Literals)---继承自Array.prototype,所以继承了大量有用的方法,比如length属性 var empty = [];var numbers = [    'zero', 'one', 'two', 'three', 'four',    'five', 's

Classical Inheritance in JavaScript

JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript's

JavaScript 学习笔记 - 对象和继承

本文是JavaScript The Good Part 有关对象和继承的学习笔记. 1. Object.create 本函数是ECMAScript 5中的标准函数,其作用是用一个对象作为原型来生成另一个对象,可以用以下的code 模拟实现. if(typeof Object.create !== 'function') { Object.create = function(proto){ var F = function(){}; if(typeof proto !== 'object'){ /

Javascript Module 模式

Javascript Module Pattern 可以说是在Javascript代码实现过程中的最佳实践方法,能够清晰地表达Javascript面向对象的概念.其核心理念是用Javascript的“类”封装私有和公有的属性和方法.它不允许开发人员定义全局变量去“污染”全局对象.通过这种模式,可以提高Web的性能,也利于维护Javascript的代码. 在Javascript程序设计语言中,函数可以作为一个模块,在某些情况下,需要创建的是单例对象,而不是创建一个类的实例.在模块的内部,公共接口可

JavaScript周报#183

This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 183May 30, 2014 Editor: Peter Cooper   Featured Introducing Socket.IO 1.0: A Leap Forward for the Real-Time Communications Library— After what looked like

Private Members in JavaScript

Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most misunderstood programming language. Some believe that it lacks the property of information hiding because objects cannot have private instance variables

JavaScript技巧手册

js小技巧 每一项都是js中的小技巧,但十分的实用! 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4.一个浏览器窗口中的DOM顺序是:window->(navigator,screen,history,location,document) 5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID号&q

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

经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 json.js也可直接复制此处的代码获取. 1 /* 2 json.js 3 2008-03-14 4 5 Public Domain 6 7 No warranty expressed or implied. Use at your own risk. 8 9 This file has been