Is JavaScript object-oriented?

JavascriptOop

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

The short answer is Yes. For more information:

From Wikipedia:

> JavaScript is heavily object-based. > Objects are associative arrays, > augmented with prototypes (see below). > Object property names are associative > array keys: obj.x = 10 and obj["x"] = > 10 are equivalent, the dot notation > being merely syntactic sugar. > Properties and their values can be > added, changed, or deleted at > run-time. The properties of an object > can also be enumerated via a for...in > loop.

Also, see this series of articles about OOP with Javascript.

Solution 3 - Javascript

Javascript is a multi-paradigm language that supports procedural, object-oriented (prototype-based) and functional programming styles.

Here is an article discussing how to do OO in Javascript.

Solution 4 - Javascript

Languages do not need to behave exactly like Java to be object-oriented. Everything in Javascript is an object; compare to C++ or earlier Java, which are widely considered object-oriented to some degree but still based on primitives. Polymorphism is a non-issue in Javascript, as it doesn't much care about types at all. The only core OO feature not directly supported by the syntax is inheritance, but that can easily be implemented however the programmer wants using prototypes: here is one such example.

Solution 5 - Javascript

JavaScript is object-oriented, but is not a class-based object-oriented language like Java, C++, C#, etc. Class-based OOP languages are a subset of the larger family of OOP languages which also include prototype-based languages like JavaScript and Self.

Solution 6 - Javascript

Yes and no.

JavaScript is, as Douglas Crockford puts it, "the world's most misunderstood programming language." He has some great articles on JavaScript that I'd strongly recommend reading on what exactly JavaScript is. It has more in common with LISP that C++.

Solution 7 - Javascript

JavaScript is a prototype-based programming language (probably prototype-based scripting language is more correct definition). It employs cloning and not inheritance. A prototype-based programming language is a style of object-oriented programming without classes. While object-oriented programming languages encourages development focus on taxonomy and relationships, prototype-based programming languages encourages to focus on behavior first and then later classify.

The term “object-oriented” was coined by Alan Kay in 1967, who explains it in 2003 to mean
> only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. (source)

In object-oriented programming, each object is capable of receiving messages, processing data, and sending messages to other objects.

For a language to be object-oriented in may include features as encapsulation, modularity, polymorphism, and inheritance, but it is not a requirement. Object-oriented programming languages that make use of classes are often referred to as classed-based programming languages, but it is by no means a must to make use of classes to be object-oriented.

JavaScript uses prototypes to define object properties, including methods and inheritance.

Conclusion: JavaScript IS object-oriented.

Solution 8 - Javascript

Unlike most object-oriented languages, JavaScript (before ECMA 262 Edition 4, anyway) has no concept of classes, but prototypes. As such, it is indeed somewhat subjective whether to call it object-oriented or not.

@eliben: Wikipedia says object-based. That's not the same as object-oriented. In fact, their article on object-based distinguishes between object-oriented languages and prototype-based ones, explicitly calling JavaScript not object-oriented.

Solution 9 - Javascript

JavaScript is a very good language to write object oriented web apps. It can support OOP because supports inheritance through prototyping also properties and methods. You can have polymorphism, encapsulation and many sub-classing paradigms.

Solution 10 - Javascript

This is of course subjective and an academic question. Some people argue whether an OO language has to implement classes and inheritance, others write programs that change your life. ;-)

(But really, why should an OO language have to implement classes? I'd think objects were the key components. How you create and then use them is another matter.)

Solution 11 - Javascript

it is a good thread. Here's some resources i like. Most people start out with prototype, jquery, or one of the top 6 libs(mootools, ExtJS, YUI), which have different object models. Prototype tries to replicate classical O-O as most people think of it

http://jquery.com/blog/2006/08/20/why-jquerys-philosophy-is-better/

Here's a picture of prototypes and functions that i refer to a lot

http://www.mollypages.org/misc/js.mp?

Solution 12 - Javascript

I am responding this question bounced from another angle.

This is an eternal topic, and we could open a flame war in a lot of forums.

When people assert that JavaScript is an OO programming language because they can use OOD with this, then I ask: Why is not C an OO programming language? Repeat, you can use OOD with C and if you said that C is an OO programming language everybody will said you that you are crazy.

We could put here a lot of references about this topic in very old books and forums, because this topic is older than the Internet :)

JavaScript has not changed for many years, but new programmers want to show JavaScript is an OO programming language. 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.

An very illustrate example: In chapter 6 of "Object-Oriented JavaScript" describe 10 manners to implement "inheritance". How many manners there are in Java? One, and in C++? One, and in Delphi (Object Pascal)? One, and in Objective-C? One.

Why is this different? Because Java, C++, Delphi and Objective-C are designed with OOP in mind, but not JavaScript.

When I was a student (in 1993), in university, there was a typical home work: Implement a program designed using a OOD (Object-oriented design) with a non-OO language. In those times, the language selected was C (not C++). The objective of this practices was to make clear the difference between OOD and OOP, and could differentiate between OOP and non-OOP languages.

Anyway, it is evidence that not all people have some opinion about this topic :)

Anyway, in my opinion, JavaScript is a powerful language and the future in the client side layer!

Solution 13 - Javascript

Hanselminutes episode 146 looks at OO Ajax. It was a good show and definitely a good show to help form an opinion.

Solution 14 - Javascript

Misko Hevery did an excellent introductory Google Tech Talk where he talks about objects in Javascript. I've found this to be a good starting point for people either questioning the use of objects in Javascript, or wanting to get started with them:

Solution 15 - Javascript

The Microsoft Ajax Client Library makes it simple to implement OO in javascript. It supports inharitence, and interface implementation.

Solution 16 - Javascript

I think a lot of people answer this question "no" because JavaScript does not implement classes, in the traditional OO sense. Unfortunately (IMHO), that is coming in ECMAScript 4. Until then, viva la prototype! :-)

Solution 17 - Javascript

I think when you can follow the same or similar design patterns as a true OO language like Java/C#, you can pretty much call it an OO language. Some aspects are obviously different but you can still use very well established OO design pattersn.

Solution 18 - Javascript

JavaScript is Object-Based, not Object-Oriented. The difference is that Object-Based languages don't support proper inheritance, whereas Object-Oriented ones do.

There is a way to achieve 'normal' inheritance in JavaScript (Reference here), but the basic model is based on prototyping.

Solution 19 - Javascript

Everything in javascript is an object - classes are objects, functions are objects, numbers are objects, objects objects objects. It's not as strict about typing as other languages, but it's definitely possible to write OOP JS.

Solution 20 - Javascript

Yes, it is. However, it doesn't support all of the features one would expect in an object oriented programming language lacking inheritance and polymorphism. This doesn't mean, however, that you cannot simulate these capabilities through the prototyping system that is avaialble to the language.

Solution 21 - Javascript

Javascript is not an object oriented language as typically considered, mainly due to lack of true inheritance, DUCK typing allows for a semi-true form of inheritance/polymorphism along with the Object.prototype allowing for complex function sharing. At its heart however the lack of inheritance leads to a weak polymorphism to take place since the DUCK typing will insist some object with the same attribute names are an instance of an Object which they were not intended to be used as. Thus adding attributes to random object transforms their type's base in a manner of speaking.

Solution 22 - Javascript

Technically it is a prototype language, but it's easy to to OO in it.

Solution 23 - Javascript

It is object oriented, but not based on classes, it's based on prototypes.

Solution 24 - Javascript

Objects in JavaScript inherit directly from objects. What can be more object oriented?

Solution 25 - Javascript

For me personally the main attraction of OOP programming is the ability to have self-contained classes with unexposed (private) inner workings.

What confuses me to no end in Javascript is that you can't even use function names, because you run the risk of having that same function name somewhere else in any of the external libraries that you're using.

Even though some very smart people have found workarounds for this, isn't it weird that Javascript in its purest form requires you to create code that is highly unreadable?

The beauty of OOP is that you can spend your time thinking about your app's logic, without having to worry about syntax.

Solution 26 - Javascript

> Is JavaScript object-oriented? > >Answer : Yes

It has objects which can contain data and methods that act upon that data. Objects can contain other objects.

  • It does not have classes, but it does have constructors which do what classes do, including acting as containers for class variables and methods.
  • It does not have class-oriented inheritance, but it does have prototype-oriented inheritance.

The two main ways of building up object systems are by inheritance (is-a) and by aggregation (has-a). JavaScript does both, but its dynamic nature allows it to excel at aggregation.

Some argue that JavaScript is not truly object oriented because it does not provide information hiding. That is, objects cannot have private variables and private methods: All members are public.

But it turns out that JavaScript objects can have private variables and private methods. (Click here now to find out how.) Of course, few understand this because JavaScript is the world's most misunderstood programming language.

Some argue that JavaScript is not truly object oriented because it does not provide inheritance. But it turns out that JavaScript supports not only classical inheritance, but other code reuse patterns as well.

Sources : http://javascript.crockford.com/javascript.html

Solution 27 - Javascript

I would say it has capabilities to seem OO. Especially if you take advantage of it's ability to create methods on an existing object (anonymous methods in some languages). Client script libraries like jquery (jquery.com) or prototype (prototypejs.org) are good examples of libraries taking advantage of this, making javascript behave pretty OO-like.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScottKoonView Question on Stackoverflow
Solution 1 - JavascriptAnthonyWJonesView Answer on Stackoverflow
Solution 2 - JavascriptEli BenderskyView Answer on Stackoverflow
Solution 3 - JavascriptjopView Answer on Stackoverflow
Solution 4 - JavascriptEeveeView Answer on Stackoverflow
Solution 5 - JavascriptmunificentView Answer on Stackoverflow
Solution 6 - JavascriptDavid MohundroView Answer on Stackoverflow
Solution 7 - JavascriptVefýsingView Answer on Stackoverflow
Solution 8 - JavascriptSören KuklauView Answer on Stackoverflow
Solution 9 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 10 - JavascriptChristian DavénView Answer on Stackoverflow
Solution 11 - JavascriptGene TView Answer on Stackoverflow
Solution 12 - JavascriptangelcerveraView Answer on Stackoverflow
Solution 13 - JavascriptAaron PowellView Answer on Stackoverflow
Solution 14 - Javascriptrodrigo-silveiraView Answer on Stackoverflow
Solution 15 - JavascripturiniView Answer on Stackoverflow
Solution 16 - JavascriptAndrew HedgesView Answer on Stackoverflow
Solution 17 - JavascriptmattlantView Answer on Stackoverflow
Solution 18 - JavascriptdsmView Answer on Stackoverflow
Solution 19 - Javascriptmatt lohkampView Answer on Stackoverflow
Solution 20 - JavascriptMikeJView Answer on Stackoverflow
Solution 21 - JavascriptbmeckView Answer on Stackoverflow
Solution 22 - JavascriptRikView Answer on Stackoverflow
Solution 23 - JavascriptVasilView Answer on Stackoverflow
Solution 24 - JavascriptPavel FeldmanView Answer on Stackoverflow
Solution 25 - JavascriptKokodokoView Answer on Stackoverflow
Solution 26 - JavascriptVinayak BevinakattiView Answer on Stackoverflow
Solution 27 - JavascriptPer Hornshøj-SchierbeckView Answer on Stackoverflow