JavaScript private public privileged access

Public

The members of an object are all public members. There are two ways for putting members in a new object.

In Constructor

function Container(param) {
    this.member = param;
}

In the prototype

This technique is used to add public methods.

Container.prototype.stamp = function (string) {
    return this.member + string;
}

Private
Private members are made by the constructor. Ordinary vars and parameters of the constructor become the private members.

function Container(param) {
    this.member = param;
    var secret = 3;
    var that = this;
}

Privileged

A privileged method is able to access private methods, variables and is itself accessible to the public method and the outside.  Privileged methods are assigned with “this” within the constructor.

function Container(param) {
    this.member = param;

    this.service = function () {
        return this.member;
    };
}

 

http://javascript.crockford.com/private.html

http://phrogz.net/JS/classes/OOPinJS.html

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: