[javascript] Is JavaScript object-oriented?

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?

This question is related to javascript oop

The answer is


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?


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


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


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.


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.


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! :-)


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.


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.


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.


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.


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


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.


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.)


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!


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:


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


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.


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++.


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


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


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.


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.


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.


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.


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


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.


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.