Ahamed the Fearless Dragon

OOJS - Public, Private and Privileged Members

June 26, 2013
Public Members and methods

The public members of the javascript object(class) are those which can be accessed by the instances of the parent object(base class). It can be illustrated by the following example

var baseClass = function(){
    this.member1 = "value"; //public member
   
    this.method1 = function(){ //public method
        // Code for the method
    }
}


The publicly accessible method can also be added using the prototype keyword also as illustrated below

baseClass.prototype.method2 = function(){
    //code goes here
}


The above methods is accessed as follows

var inherClass = new baseClass();
inherClass.method1(); // Method will be called
inherClass.method2(); // Method will be called

Private Members and methods

The private members and methods are those which cannot be accessed by the instances created from the base class. The private members can be created by the normal variable creation and normal function creation. When the this keyword is eliminated within the class then the member or method is said to be as private members.

var baseClass = function(){
    var member1 = "value"; //private member
   
    var method1 = function(){ //private method
        // Code for the method
    }
}


var inherClass = new baseClass();
inherClass.method1() // Will throw error


So here the private member cannot be accessed through the instance created from the base class.

Privileged methods

The public methods which can access the private members are called the privileged methods.

var baseClass = function(){
    var member1 = "value"; //private member
   
    var method1 = function(){ //private method
        // Code for the method
    }
    this.privMethod = function(){
        alert(member1); // Here private member is accessed within in the public method
    }

}

The privileged method can be accessed using the following ways

var inherClass = new baseClass();
inherClass.privMethod(); // Will alert the value of the private member.

So here the private member is accessed through the public method because the scope of the variable member1 is localized to the method using the concept of the closures so the execution of the privMethod doesn't throw any errors.


 

Difference Between Call & Apply

June 25, 2013
The Difference Between Call and Apply in Javascript

One very common thing that trips me up when writing Javascript is knowing when to use call and when to use apply. If you're wondering what these methods are, or don't know how scope works in JavaScript, then it might make sense to read the Javascript Guide first.

Let's look at some ways we might want to use them:

var person1 = {name: 'Marvin', age: 42, size: '2xM'};
var person2 = {name: 'Zaphod', age: 42000000000, size: '1x...

Continue reading...
 

Centering Fixed width DIV horizontally and vertically

August 24, 2011

For fixed size :

#center{
   
width:500px;
   
height:300px;
   
position:absolute;
   
top:50%;
   
left:50%;
   
margin:-150px 0 0 -250px;
}

If you want to resize your div with the screen you can use this technique which works for div too.


Continue reading...
 

Javascript Special Character validation

August 24, 2011
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;

   
function check(e) {
   
var keynum
   
var keychar
   
var numcheck
   
// For Internet Explorer
   
if (window.event)
   
{
    keynum
= e.keyCode
   
}
   
// For Netscape/Firefox/Opera
   
else if (e.which)
   
{
    keynum
= e.which
   
}
    keychar
= String.fromCharCode(keynum)
   
if ((keychar.toString()).search(mikExp) == 0)
   
{

   
return false;
   
}
   
else {
   
return true;
   
}
   
}

   
<input type="text" name="display1" id="display1" v...

Continue reading...
 

Blog

August 24, 2011


Continue reading...
 

About Me


Ahamed Mukthaar I am Senior Software Engineer in a Pvt concern. Having an overall experience of about 5 years in the field of IT industry. Having good knowledge in Javascript, Node Js, Jquery, HTML, CSS, XPATH, etc..

Categories

Blog Archive

Make a Free Website with Yola.