OOP JavaScript: Accessing Public Methods in Private Methods

As you know in JavaScript when you define a variable with the special word “var” the scope of this variable is within the function. So when you simply wite “var a = 5” the variable named “a” has a global scope and can be accessed in any function in the global scope.

var a = 5;
 
function f() { return a; } // returns 5

Thus f will return the value of “a” which equals to 5. You can also change the value of the global variable in the function body.

var a = 5;
function f() { a = 10; return a; }
console.log(a); // equals to 10

Now after we call the function f the value of “a” will equal to 10. This is because we reference the global variable “a” into the function body without using the keyword “var”. This means that if you put the “var” keyword the variable “a” inside the function body is no longer the same variable as the variable defined outside the body. It becames “local” and it’s visible only inside the function.

Objects & JavaScript

Actually in JavaScript functions can be also objects. Any variable defined with the “var” keyword becomes private, because it’s only visible inside the function body, while by using the “this” keyword we can define global variables visible outside the function body. Let’s see na example.

var f = function() 
{
	var a = 10;
	this.b = 5;
}
 
var myfunc = new f();
myfunc.a; // is undefined because a is private
myfunc.b; // equals to 5 because b is public

Public and Private Methods in JavaScript

As you may know in JS you can define functions inside other functions. Actually this is how you can define classes in JavaScript.

var myClass = function() 
{
	var f = function() {
		return 10;
	}
}

But this in fact defines a local (private) function into myClass and we cannot access it from the outside world. Here’s a fully functional class with one public and one private method.

var myClass = function()
{
	// public method
	this.a = function() {
		return 10;
	}
 
	// private method
	var b = function() {
		return 5;
	}
}
 
var c = new myClass();
c.a(); // will return 10
c.b(); // is undefined, because is private

Accessing Private Methods from Public Methods

Easily we can access private methods from public ones.

var myClass = function()
{
	// public method
	this.a = function() {
		return b();
	}
 
	// private method
	var b = function() {
		return 5;
	}
}

However accessing public mtehods in private ones is more difficult. That’s because we cannot use “this” into the private methods, just because “this” refers to the private method
itself and not to the global method, which is “myClass” in our case.

var myClass = function()
{
	// public method
	this.a = function() {
		return 10;
	}
 
	// private method
	var b = function() {
		return this.a(); // this will result in an error
	}
}

Accessing Public Methods from Private Methods

To access public methods in private methods you need to define a variable that points to the global “this” object.

var myClass = function()
{
	var self = this; 
 
	// public method
	this.a = function() {
		return 10;
	}
 
	// private method
	var b = function() {
		return self.a(); // this will return 10
	}
}

This variable gives you the bridge between private methods and global “this” pointer.

11 thoughts on “OOP JavaScript: Accessing Public Methods in Private Methods

  1. Thank you for the great article. It’s simple about constructors and to the point on OOP. The main reason I came here was to see if the best way to access public methods inside my private functions.

  2. In private method ‘this’ refers to the global object, it is ‘window’ in browser or ‘global’ in server, not private method itself.

  3. Doesn’t mention prototype. How are variables or functions factored out into the prototype for a constructor? So confusing.

  4. Thanks, was going crazy trying to figure out how to access a global variable from a private function.

Leave a Reply

Your email address will not be published. Required fields are marked *