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?
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?
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.
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.
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.
The Microsoft Ajax Client Library makes it simple to implement OO in javascript. It supports inharitence, and interface implementation.
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.
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.)
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.
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.
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.
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.
A recent Hanselminutes (http://www.hanselminutes.com/default.aspx?showID=146) looks at OO AJAX, it was a good show and definately a good show to help form an opinion.
yes it is. however it doesnt support all of the features one would expect in an object oriented programming language lacking inheritence and polymorphism. this doesnt mean however that you cannot simulate these capabilities through the protyping system that is avaialble to the language.
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.
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.
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.
It is object oriented, but not based on classes, it's based on prototypes.
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++.
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.
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
Objects in javascript inherit directly from objects - wjat can be more object oriented?
I am responding this question bounced from other one.
This is a eternal topic and we could open a flame in a lot of forums.
When people assert that JS is an OOP because they can use a OOD with this, then i ask: Why is not C an OOP? Repeat, you can use OOD with C and if you said that C is an OOP 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 internet :)
JS does not changed since years, but new programmers want sign JS as a OOP. Why? JS is a powerful language, but is not a OOP.
OOP must has 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 Obetive-C? One.
Why this different? Because Java, C++, Delphi and Obetive-C are designed with OOP in main, but JS no.
when i was student ( in 1993 ), in university, there was a typical home work: Implement a program designed using a OOD (Object-oriented design) with a not OOP language. In this times, the language selected was C (no C++). Objective of this practices was had a clear difference between OOD and OOP, and could difference between OOP and not OOP languages.
Anyway, is evidence that not all people has some opinion about this topic :)
Anyway, in my opinion, JS is a powerful language and future on client side layer!
Apologies for my poor English.