JavaScript is a prototype-oriented language.
It can build actual objects from a constructor function and it has almost any feature that any object could have:
- Constructor.
- Methods (i.e. functions in JavaScript).
- Properties (since ECMA-Script 5, "getters/setters").
- Instances.
In JavaScript, any object has a prototype, including functions. The prototype itself is a rudimentary way of adding object members to any newly created instance of the whole object.
var constructor = function() { };
constructor.prototype.text = "hello world";
alert(new constructor().text); // This alerts hello world
Why JavaScript isn't an object-oriented programming (scripting) language? Because it has no feature that fits the requirements of the definition of object-oriented programming:
- Polymorphism: No. You can change the behavior of a prototype member, but this is just reusing the identifier. You aren't able to access the previous implementation of the member in a pseudo-derived object.
- Inheritance: Not at all. Maybe prototype chain might be comparable to inheritance but JavaScript (ECMA-Script 5.x or earlier versions) has no syntax-based inheritance like other OOP-based languages (i.e. Java, C#, Ruby, Python, VisualBasic.NET, ...).
- Encapsulation. Yes, of course, but there's no way to create actual private or internal object members.
Perhaps I forgot to mention some other detail, but I honestly believe that this is a good summary.
Update and summary
The core difference is an object-oriented programming language has the features that an object-oriented paradigm must have in order to be considered an object-oriented programming language. Thus, JavaScript, for now, isn't an actual object-oriented programming language because it lacks actual polymorphism and inheritance.
Update: Does ES2015 and above changed the situation?
Esthetically speaking yes, ES2015 and above has a major improvement that let consider a not fully but more closer to an object-oriented programming: syntactic sugar to call to the super class.
For example:
class A {
doStuff() {
console.log("hello world");
}
}
class B extends A {
doStuff() {
super.doStuff();
console.log("...and goodbye!");
}
}
This is polymorphism. A more specialized class can override its base class to both completely change a function behavior or do what the base was already doing, adding new code to the function.
BTW, ES2015 and above still lacks true encapsulation: where are access modifiers like private
or public
here? Nowhere.
And, at the end of the day, ES2015 and above implement class-based OOP but it's still a syntactic sugar layer on top of ECMAScript 5.x... The above code still works with prototypes under the hoods and it works the same way as if you would code it in ECMAScript 5.x:
function A() {
}
A.prototype.doStuff = function() {
console.log("hello world");
};
function B() {
}
B.prototype = Object.create(A.prototype);
B.prototype.doStuff = function() {
A.prototype.doStuff.call(this);
console.log("...and goodbye!");
};
Let's hope I'll need to update this answer again because ES2020 has already proposed access modifiers and we'll be able to consider JavaScript another language which fully-supports object-oriented programming!