Basic OOP Concepts in Javascript

Object-Oriented Programming (OOP) is a new and popular programming trend, is supported by almost the programming languages today, and JavaScript is not an exception, too. This post provides some basic concepts in using OOP in Javascript.


Sampled by © JavaScriptBank.com

Object-Oriented Programming is currently one of the most prominent aspects of programming. Many programming courses teach OOP, and PHP5 supports proper OOP. While Javascript as a language is not inherently well-suited for full fledged OOP, it is possible to make it work.

Here is a basic primer in using OOP in Javascript.

Methods vs Objects

In Javascript, both general methods and objects are defined using the function keyword. For example, the method and the object definitions below are both denoted by function.

function add( a, b ) {
	return a+b;
}

function sum( a, b ) {
	this.total = a+b;
}

The difference between a method call and an object instantiation call is the keyword new. The difference is illustrated in the example below:

var addition = add( 1, 2 );
var summation = new sum( 1, 2 );

Fields

In OOP in Javascript, objects have fields that store data. You have already seen an example of this earlier in the sum object, which stores the sum of a+b as total. All of these fields are public - that is to say, modifiable by elements other than the object itself. For example:

var summation = new sum( 1, 2 );
alert(summation.total); // shows 3
summation.total = 5;
alert(summation.total); // shows 5

Methods in Objects

Next, objects can declare their own methods. These methods can be defined within the object definition, or outside of the definition and assigned to a variable within the object definition.

This provides an example of the former:

function sum( a, b ) {
	this.total = add( a, b );
	this.print = function() {
		print(this.total);
	}
}

and this provides an example of the latter:

function sum( a, b ) {
	this.total = add( a, b );
	this.print = print;
}

function print( total ) {
	print(total);
}

Inheritance

Finally, a major part of OOP is inheritance. Inheritance is when a "subclass" takes on all the methods of a "superclass". Inheritance in Javascript is done using two lines:

this.inheritFrom = ;
this.inheritFrom();

Here is an example of the subclass retaining the methods of the superclass while adding methods of its own.

function super() {
	this.alert = function() {
		alert("Hello!");
	}
	this.alert2 = function() {
		alert("Hello2!");
	}
}
function sub() {
	this.inheritFrom = super;
	this.inheritFrom();

	this.alert3 = function() {
		alert("Hello3!");
	}
}
var c = new sub();
c.alert(); // alerts "Hello!"
c2.alert2(); // alerts "Hello2!"
c2.alert3(); // alerts "Hello3!"

While this primer is not necessarily in-depth, it has hopefully given you a basic understanding how OOP works in Javascript.

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Insights for Advanced Zooming and Panning in JavaScript Charts
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website


Top view articles
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Beautiful Christmas Countdown Timers
Top 10 Best JavaScript eBooks that Beginners should Learn
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com