Is object oriented JavaScript?

There were some questions about whether JavaScript is an object-oriented language. Even the statement "just because the language has objects does not make it OO."

Is JavaScript an Object Oriented Language?

+55
javascript oop
Sep 20 '08 at 7:03
source share
27 answers

IMO (and this is just an opinion) a key characteristic for an object-oriented language will be that it will support polymorphism . Almost all dynamic languages ​​do this.

The next sign will be encapsulation , and this is very easy to do in Javascript.

However, in the minds of many, this is inheritance (in particular, implementation inheritance), which suggests a balance as to whether the language is called object-oriented.

Javascript does provide fairly simple means for inheriting an implementation through prototyping, but this is through encapsulation.

So, if your criteria for object orientation is the classic triple of polymorphism, encapsulation and inheritance, then Javascript fails.

Change An additional question has been added: "How does prototype inheritance sacrifice encapsulation?" Consider this example of a non-prototype approach: -

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

The _value attribute is encapsulated; it cannot be directly modified by external code. We could add a mutator to the class to change it in a way that is completely controlled by the code that is part of the class. Now consider the prototype approach to the same class: -

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

Well, it's broken. Since the function assigned to getValue is no longer in scope with _value, it cannot access it. We will need to raise the value of _value to the this attribute, but this will make it accessible outside the control of the code written for the class, so encapsulation will be broken.

Despite this, my voice still remains that Javascript is object oriented. What for? Since this OOD , I can implement it in Javascript.

+53
Sep 20 '08 at 17:14
source share

Short answer: Yes. For more information:

From Wikipedia :

JavaScript is heavily object-based. Objects are associative arrays, supplemented by prototypes (see below). The object property names are associative array keys: obj.x = 10 and obj ["x"] = 10 are equivalent, the dot notation being just syntactic sugar. Properties and their values ​​can be added, changed, or deleted at runtime. The properties of an object can also be listed through a loop.

Also see this series of articles about OOP with Javascript.

+36
Sep 20 '08 at 7:08
source share

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

Here is an article that discusses how to do OO in Javascript.

+24
Sep 20 '08 at 7:08
source share

Languages ​​do not have to behave exactly like Java in order to be object oriented. Everything in Javascript is an object; compare with C ++ or earlier Java, which to some extent is widely regarded as object-oriented, but still based on primitives. Polymorphism is not a problem in Javascript, since it does not care about types at all. The only main OO function that is not directly supported by the syntax is inheritance, but it can be easily implemented, however the programmer wants to use prototypes: here is one such example.

+19
Sep 20 '08 at 7:14
source share

JavaScript is object-oriented, but not an object-oriented language based on classes, such as Java, C ++, C #, etc. Class-based OOP languages ​​are a subset of the larger number of OOP languages ​​that also include prototypes such as JavaScript and Self.

+10
22 sept. '08 at 17:41
source share

Yes and no.

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

+10
Sep 22 '08 at 17:55
source share

JavaScript is a prototype-based programming language (probably a prototype-based scripting language is a better definition). It uses cloning, not inheritance. A prototype-based programming language is an object-oriented programming style without classes. While object-oriented programming languages ​​stimulate the development of attention to taxonomy and relationships, prototype-based programming languages ​​encourage you to focus on behavior first and then classify later.

The term "object-oriented" was coined by Alan Kay in 1967, which explains this in 2003 as meaning

only messaging, local holding and protection and hiding the state process, as well as extreme late binding of all things. (source)

In object-oriented programming, each object is able to receive messages, process data, and send messages to other objects.

For a language that needs to be object oriented, functions like encapsulation, modularity, polymorphism, and inheritance may be included, but this is not a requirement. Object-oriented programming languages ​​that use classes are often referred to as classification-based programming languages, but it is by no means necessary to use classes for object-oriented programming.

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

Conclusion: JavaScript IS is object oriented.

+8
Sep 30 '08 at 10:05
source share

Unlike most object-oriented languages, JavaScript (before ECMA 262 Edition 4, anyway) does not have the concept of classes, but prototypes. Thus, it is really somewhat subjective whether it should be called object-oriented or not.

@eliben: Wikipedia talks about an object. This is not the same as object oriented. In fact, their article on object-oriented distinguishes between object-oriented languages ​​and prototypes that explicitly invoke JavaScript, rather than object-oriented.

+6
Sep 20 '08 at 7:55
source share

JavaScript is a very good language for writing object-oriented web applications. It can support OOP because it supports inheritance by prototyping also properties and methods. You can have polymorphism, encapsulation, and many subclass paradigms.

+3
Sep 20 '08 at 7:11
source share

This, of course, is a subjective and academic question. Some people argue that OO should implement classes and inheritance; others write programs that change your life .; -)

(But actually, why does OO need to implement classes? I think that objects were key components. How you create them and then use them is another matter.)

+2
Sep 20 '08 at 7:48
source share

This is a good thread. Here are some resources that I like. Most people start with a prototype, jquery, or one of the top 6 libs (mootools, ExtJS, YUI) that have different object models. The prototype is trying to reproduce the classic OO, as most people think about it.

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

Here is an image of prototypes and functions that I name a lot

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

+2
Sep 30 '08 at 18:47
source share

I answer this question by jumping from a different angle.

This is an eternal topic, and we can open a fiery war in many forums.

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

We could add a lot of links to 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 that JavaScript is an OO programming language. What for? JavaScript is a powerful language, but not an OO programming language.

The OO programming language must have objects, a method, a property, classes, encapsulation, aggregation, inheritance, and polymorphism. You can implement all these points, but JavaScript does not have them.

A very illustrative example: Chapter 6 of “Object Oriented JavaScript” describes 10 manners for implementing “inheritance”. How many manners exist in Java? One, but in C ++? Alone, and in Delphi (Object Pascal)? One, but in Objective-C? One.

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

When I was a student (in 1993), the university had a typical homework: run a program developed using OOD (Object-oriented design) with a language other than OO. At that time, the chosen language was C (not C ++). The purpose of this practice was to clearly define the difference between OOP and OOP and can distinguish between OOP and languages ​​other than OOP.

In any case, this indicates that not all people have some opinion on this topic :)

In any case, in my opinion, JavaScript is a powerful language and the future is at the client level!

+2
Oct 30 2018-10-10
source share

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

+1
Sep 20 '08 at 9:59
source share

Misko Hevery did an excellent introductory Tech Tech Tech, where he talks about objects in Javascript. I found this to be a good starting point for people who either interrogate the use of objects in Javascript or want to start with them:

+1
Jan 29 '12 at 17:43
source share

The Microsoft Ajax client library simplifies the implementation of OO in javascript. It supports fuzziness and interface implementation.

0
Sep 20 '08 at 7:15
source share

I think many people answer this question “no,” because JavaScript does not implement classes in the traditional sense of OO. Unfortunately (IMHO), which is included in ECMAScript 4. Until then, viva la prototype !:-)

0
Sep 20 '08 at 7:31
source share

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

0
Sep 20 '08 at 7:58
source share

JavaScript is based on objects, not object oriented. The difference is that object-oriented languages ​​do not support proper inheritance, while object-oriented ones.

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

0
Sep 20 '08 at 8:41
source share

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

0
Sep 20 '08 at 9:54
source share

Yes it is. However, it does not support all the functions that one would expect in an object-oriented programming language, devoid of inheritance and polymorphism. This does not mean, however, that you cannot simulate these features through a prototyping system that is available for the language.

0
Sep 20 '08 at 14:10
source share

Javascript is not an object-oriented language, as is commonly believed, mainly due to the lack of true inheritance. DUCK typing allows a semi-true form of inheritance / polymorphism along with Object.prototype, which allows for complex sharing of functions. At the heart of all this, however, the lack of inheritance leads to a weak polymorphism that occurs after the DUCK team insists that some object with the same attribute names is an instance of an object that they are not intended to be used. Thus, adding attributes to a random object transforms their type base in the form of speech.

0
Sep 21 '08 at 23:31
source share

Technically, it is a prototype language, but it is easy for OO in it.

0
Sep 22 '08 at 17:48
source share

Object oriented, but not class based, it is prototype based.

0
Sep 22 '08 at 17:50
source share

Objects in JavaScript are inherited directly from objects. What could be more object oriented?

0
Nov 26 '08 at 20:46
source share

For me personally, the main attraction of OOP programming is the ability to have autonomous classes with unexposed (private) internal work.

What doesn’t bother me with Javascript is that you cannot even use function names, because you run the risk of having the same function name elsewhere in any of the external libraries that you use.

Even if some very smart people have found workarounds for this, is it not strange that Javascript in its pure form requires the creation of code that is very unreadable?

The beauty of OOP is that you can spend your time thinking about your application logic without worrying about syntax.

0
May 11 '12 at 14:42
source share

Is object oriented JavaScript?

Answer: Yes

It has objects that can contain data and methods that affect this data. Objects may contain other objects.

  • It has no classes, but it has constructors that execute which classes, 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 object systems are inheritance (is-a) and aggregation (has-a). JavaScript does both, but its dynamic nature allows it to excel in aggregation.

Some argue that JavaScript is not object oriented because it does not hide information. That is, objects cannot have private variables and private methods: all members are publicly available.

But it turns out that JavaScript Objects can have private variables and private methods. (Click here to learn how to do this.) Of course, few people understand this because JavaScript is the most obscure programming language in the world.

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

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

0
May 14 '12 at 11:51
source share

I would say that he has the ability to seem like OO. Especially if you take advantage of his ability to create methods on an existing object (anonymous methods in some languages). Client script libraries such as jquery (jquery.com) or prototype (prototypejs.org) are good examples of libraries that use this, making javascript very nice OO-like.

-one
Sep 20 '08 at 7:41
source share



All Articles