A Simple Example About Privileged Methods in JavaScript, privilegedmethods

Source: Internet
Author: User

A Simple Example About Privileged Methods in JavaScript, privilegedmethods
Douglas Crockford classified the "class methods" in JavaScript into three types:Private,PrivilegedAndPublic.
Public methodsHave 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 arePrivileged methods? To understand that, we shoshould have a short review of how we implement public and private methods in JavaScript first.
Recall that, unlike ages like C ++, JavaScript does not have "class". Moreover, it does not haveAccess specifiersLikePublic,ProtectedAndPrivate. Lack of these features make the creation of private and public methods less clear than it shoshould be.
To writePublic methodIn JavaScript, we make use of. PrototypeProperty 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 learned that FootballPlayer. prototypeIs an object that is built automatically when the function is created. Object constructed with the FootballPlayer, Messi, search through his _ Proto _ chainWhen we tell him to kick. obviusly FootballPlayer. prototypeIs on the chain so Messi knows how to kick. All objects created by Constructor functionShare the same FootballPlayer. prototypeSo they all can invoke Public methodKick!
Private methodsAre kinda tricky. We declare private variable in ConstructorUsing 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 CannotAccess them.
However, Privileged methodsCan access Private membersDue to the existence 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 methodsEasily by using the keyword This!

Read More:
Prototype and Constructor in JavaScript by RedcapCoder
Private Members in JavaScript by Douglas Crockford

High score resume Translation

System benefits: xxx is a complete content management system, using this system enables developers to do not have to development projects in the content management page, so as to shorten the development cycle of the project. for example, XXX Web site, we are only one and a half months completes the site's data structure and the page is displayed.
Description: responsible for duties on the system for the second development, allowing the system to be able to support multiple enterprise application multi-project hybrid. and the second to the system during the development process itself, some basic features of the original bug fixes. on xxx labels and class well, know how to configure the varous features of the system.
For the second development is the main content:
1. managers can customize on what groups open which backend management functionality.
2. the different project managers can manage only the account they are projects and resources.
3. when secured folder, to improve the level of permissions that are required.
4. for non-administrators, shielding it some features to tie in with the implementation of the second.
5. improving Advanced structured templates, PWS dynamic value of functionality.
6. repair your system path, low-privileged account to access a folder will no longer work correctly after you use the system bugs.

Descriptions: participates in the development database structure, writing project about 80% of interactivity and the page is displayed (depending wha... the remaining full text>
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.