Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JavaScript object-oriented?

Tags:

javascript

oop

There have been some questions about whether or not JavaScript is an object-oriented language. Even a statement, "just because a language has objects doesn't make it OO."

Is JavaScript an object-oriented language?

like image 293
ScottKoon Avatar asked Sep 20 '08 07:09

ScottKoon


People also ask

Is JavaScript functional or object oriented?

What is JavaScript? JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it's used in many non-browser environments as well.

Why is JavaScript not object oriented?

Why? JavaScript is a powerful language, but is not an OO programming language. An OO programming language must have objects, method, property, classes, encapsulation, aggregation, inheritance and polymorphism. You could implement all this points, but JavaScript has not them.

Should you use OOP in JavaScript?

So… when should you use OOP? # If you need a simple guideline: whenever you're going to being creating more than one or two of an item that has some shared properties and some unique ones.

Why JavaScript is object based?

JavaScript is a prototype-based programming language. A prototype-based programming language is a style of object-oriented programming that uses functions as constructors for classes . Although JavaScript has a keyword class, it has no class statement. Also, it employs cloning and not inheritance.


1 Answers

IMO (and it is only an opinion) the key characteristic of an object orientated language would be that it would support polymorphism. Pretty much all dynamic languages do that.

The next characteristic would be encapsulation and that is pretty easy to do in Javascript also.

However in the minds of many it is inheritance (specifically implementation inheritance) which would tip the balance as to whether a language qualifies to be called object oriented.

Javascript does provide a fairly easy means to inherit implementation via prototyping but this is at the expense of encapsulation.

So if your criteria for object orientation is the classic threesome of polymorphism, encapsulation and inheritance then Javascript doesn't pass.

Edit: The supplementary question is raised "how does prototypal inheritance sacrifice encapsulation?" Consider this example of a non-prototypal approach:-

function MyClass() {     var _value = 1;     this.getValue = function() { return _value; } } 

The _value attribute is encapsulated, it cannot be modified directly by external code. We might add a mutator to the class to modify it in a way entirely controlled by code that is part of the class. Now consider a prototypal approach to the same class:-

function MyClass() {   var _value = 1; } MyClass.prototype.getValue = function() { return _value; } 

Well this is broken. Since the function assigned to getValue is no longer in scope with _value it can't access it. We would need to promote _value to an attribute of this but that would make it accessable outside of the control of code written for the class, hence encapsulation is broken.

Despite this my vote still remains that Javascript is object oriented. Why? Because given an OOD I can implement it in Javascript.

like image 118
AnthonyWJones Avatar answered Sep 23 '22 06:09

AnthonyWJones