Introduction
JavaScript is the language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.
Before moving further let us look at the previous articles of the series:
- Voice of a Developer: JavaScript Data Types - Part One
- Voice of a Developer: JavaScript Objects - Part Two
- Voice of a Developer: JavaScript Engines - Part Three
- Voice of a Developer: JavaScript Common Mistakes - Part Four
- Voice of a Developer: Editors - Part Five
- Voice of a Developer: VSCode - Part Six
- Voice of a Developer: Debugging Capabilities of VSCode - Part Seven
We use OOPS in programming languages like C++, C#, Java. It is fundamental to our programming patterns.
Yes it is, but many developers differ because there is no “class” keyword in it. I believe JavaScript has all the ingredients to be an OOP language. We can read in detail OOP in JavaScript.
Encapsulate means “enclose (something) in or as if in a capsule”. It refers to enclosing all the functionalities within that object, so it is hidden from the rest of the application. There are many ways to create Objects (refer http://www.c-sharpcorner.com/article/voice-of-a-developer-javascript-objects/ Voice of a Developer – part 2 for details)
Below is the ubiquitous object literal pattern, my favorite way to declare objects:
Q: Is JavaScript an object-oriented language?
Encapsulation
- var myObj = {
- name: "Sumit",
- profession: "Developer",
- getDetails: function(n, p) {
- return n + ' ' + p;
- }
- };
Inheritance
Prototype property
Example
- function baseFunction() {};
- var func1 = new baseFunction();
- baseFunction.myOwnProperty = 'my own emunerable property';
- baseFunction.prototype.testInheritanceProperty = 'other instances can inherit it';
- var func2 = new baseFunction();
- var func3 = new baseFunction();
- console.log(func1.myOwnProperty);
- console.log(func2.testInheritanceProperty); // other instances can inherit it
- console.log(func3.testInheritanceProperty); // other instances can inherit it
Prototype attribute
JS runtime first look property on the object. If not found, then looks for the property on the object’s prototype—the object it inherited its properties from. This is called as prototype chaining. JavaScript uses this prototype chain to look for properties and methods of an object.
- Object.prototype.newProperty = 100;
- var obj = {} // creates a new Object
- obj.newProperty; // 100

Q: Why we get undefined while accessing a property?
Now I believe you know the answer, i.e., if a property does not exist in object or parent or onwards then JS runtime returns undefinedAbstraction
Q: Is there a support in Javascript on Abstraction?
Let us see an example of abstraction
- var Logger = {
- log: function(log) {}
- } //Logger object which has a constructor function
- var ConsoleLogger = function() {}; //It is a blank function
- ConsoleLogger.prototype = Object.create(Logger);
- ConsoleLogger.prototype.log = function(log) {
- console.dir(log);
- }
Output

Summary
OOP is there in JavaScript, it is up to us how we leverage it. Please share your feedback and comments.
Read more articles on JavaScript
Amit DavePosted May 3, 2016, 6:56 AM
Nice article!
Sonu ChaudharyPosted May 2, 2016, 5:58 AM
Thanks for sharing
Gowtham RajamanickamPosted May 2, 2016, 2:45 AM
good one..
Debasis SahaPosted May 1, 2016, 1:47 PM
Good One..
NitinPosted May 1, 2016, 1:44 PM
Good one
Kuppurasu NagarajPosted May 1, 2016, 12:51 PM
Nice Sharing..