A Simple Example About Privileged Methods in JavaScript

來源:互聯網
上載者:User

標籤:function   mil   erro   com   method   _id   href   use   world   

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 functionFootballPlayer.prototype.kick = function(){ alert("kick!"); }; // create a public method kick in .prototypevar 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 methodvar obj = new func(1,2);alert(obj.getB()); // privileged method can access private balert(obj.getB2()); // error: public method cannot access private bobj.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

A Simple Example About Privileged Methods in JavaScript

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.