JavaScript · Programming

JavaScript Global, Private, Public and Privileged members

Javascript is an object based language. Everything is in JavaScript is an object including methods, variables and native types like String, Math etc.

Two ways to create custom objects in Javascript are:

  1. By using JavaScript function.
  2. By using JavaScript object literal notation.

Global scoped objects


Global objects are part of the window object.

  1. These are the objects that are declared outside any function or literal.
  2. Also, the variables declared inside a function without the var keyword, becomes a global object.


/*Global object demo*/
var globalVar1 = 'globalVar1';


function sayHello() {
globalVar2 = 10; //becomes part of the window object
var bbbb = 20; //becomes part of the sayHello function object
}


function sayMe() {
alert(globalVar2); //will print 10
alert(bbbb); //will throw ReferenceError : bbbb is not defined
}


sayHello();
sayMe();

Private members.

  1. Private fields are variables created inside a function using the var keyword.
  2. Private methods are just inner functions, created using the var keyword or directly.
  3. We CANNOT access private variables using “this” keyword.
  4. Private methods cannot be accessed from anywhere outside the object.
  5. Private methods can call both private and public variables.


function testScope() {
/*private to this object. You CANNOT access private variables using "this" keyword.*/
var privateVar = 'This is private';
this.publicVar = 'i am public';


/*
Private method in JS is just an inner function.
But they cannot be accessed from anywhere outside the object.
Private methods can call both private and public variables
*/


var myPrivateMethod1 = function() {alert('myPrivateFunc1');alert(publicVar);}
function myPrivateMethod2() {alert('myPrivateFunc2');alert(privateVar);}
}

Public members

  1. Public fields are created using the “this” keyword inside a function Constructor.
  2. Public methods are added using the prototype object on function Constructor.
  3. Public members, can be added deleted, or changed by any external function.


function publicScope() {
//public field, can be added deleted, or changed by any external function
this.public = 'This is public';
}


//public method
publicScope.prototype.publicMethod = function(){alert('I am public method');}


var testPublicScope = new publicScope();
alert(testPublicScope.public);
testPublicScope.publicMethod();

Privileged members

  1. Privileged methods are declared using the “this” keyword. These are public methods.
  2. Privileged methods allow us to call private methods of the parent objects from outside the class.
  3. Privileged methods can call private and public variables directly.
  4. Privileged methods can be deleted or replaced but not altered


function privFunction() {
var private = 'private';
var public = 'public';
var myPrivateMethod = function(){alert('private method');};


//privileged method
this.privMethod = function() {
alert(public);// call public
alert(private); //call private field
myPrivateMethod(); //call private method
}
}


var pfDemo = new privFunction();
pfDemo.privMethod();

What is returned if we call a private variable and public variable directly?

  1. In case of private field, undefined is returned.
  2. In case of private method, error is thrown.
  3. In case of public field, value is returned.


function testScope() {
this.public = 'This is public';
var private = 'This is private';
var myPrivateMethod = function(){alert('private method');};
}


ps = new testScope();


alert(ps.public); // alerts This is public
alert(ps.private); // alerts undefined
ps.myPrivateMethod(); // error is thrown

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