Is JavaScript Object-Oriented

Is JavaScript object-oriented?

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.

Is javascript an object oriented language?

JavaScript does have objects. I would say it's a hybrid: interpreted, functional, object-oriented, and dynamic.

I think these characteristics are very important, indeed. They are what makes it a good language, one that's more important every day.

IS OOP possible in Javascript?

OOP is definitely possible. While Javascript doesn't have "classes" like most OO languages do, what it does have is called "prototypes". Basically, objects are defined in terms of other objects rather than classes. (Objects can also emulate classes to some degree, for those who can't wrap their minds around prototypal inheritance.)

One could argue JS's OO capabilities exceed those of most languages, considering objects play an even more essential role than in languages with classes.

Difference between object oriented and object based language

Wikipedia says:

In computer science, the term object-based has two different senses:

  • A somehow limited version of object-oriented programming, where one or more of the following restrictions applies: (a) There is no implicit inheritance, (b) there is no polymorphism, (c) only a very reduced subset of the available values are objects (typically the GUI components).

  • Prototype-based systems (that is, those based on "prototype" objects that are not instances of any class).

Javascript is object based.

Beginner JavaScript OOP vs Functional

There's no correct definition for what is and isn't "functional", but generally functional languages have an emphasis on simplicity where data and functions are concerned.

Most functional programming languages don't have concepts of classes and methods belonging to objects. Functions operate on well-defined data structures, rather than belonging to the data structures.

The first style _.map is a function in the _ namespace. It's a standalone function and you could return it or pass it to another function as an argument.

function compose(f, g) {
return function(data) {
return f(g(data));
}
}

const flatMap = compose(_.flatten, _.map);

It's not possible to do the same for the second style, because the method instance is intrinsically tied to the data used to construct the object. So I'd say that the first form is more functional.

In either case, the general functional programming style is that data should be the final argument to the function, making it easier to curry or partially apply the earlier arguments. Lodash/fp and ramda address this by having the following signature for map.

_.map(func, data);

If the function is curried, you can create specific versions of the function by only passing the first argument.

const double = x => x * 2;
const mapDouble = _.map(double);

mapDouble([1, 2, 3]);
// => [2, 4, 6]

What does it mean that Javascript is a prototype based language?

Prototypal inheritance is a form of object-oriented code reuse. Javascript is one of the only [mainstream] object-oriented languages to use prototypal inheritance. Almost all other object-oriented languages are classical.

In classical inheritance, the programmer writes a class, which defines an object. Multiple objects can be instantiated from the same class, so you have code in one place which describes several objects in your program. Classes can then be organized into a hierarchy, furthering code reuse. More general code is stored in a higher-level class, from which lower level classes inherit. This means that an object is sharing code with other objects of the same class, as well as with its parent classes.

In the prototypal inheritance form, objects inherit directly from other objects. All of the business about classes goes away. If you want an object, you just write an object. But code reuse is still a valuable thing, so objects are allowed to be linked together in a hierarchy. In javascript, every object has a secret link to the object which created it, forming a chain. When an object is asked for a property that it does not have, its parent object will be asked... continually up the chain until the property is found or until the root object is reached.

Each function in JavaScript (which are objects themselves) actually has a member called "prototype", which is responsible for providing values when an object is asked for them. Having this member allows the constructor mechanism (by which objects are constructed from functions) to work. Adding a property to the prototype of a function object will make it available to the constructed object, as well as to all of the objects which inherit from it.

Advantages

There may not be a hard and fast rule as to why prototypal inheritance is an advantageous form of code-reuse. Code reuse itself is advantageous, and prototypal inheritance is a sensible way of going about it. You might argue that prototypal inheritance is a fairly simple model of code reuse, and that code can be heavily reused in direct ways. But classical languages are certainly able to accomplish this as well.

Sidenote: @Andrew Hedges makes a good point, that there are actually many prototypal languages. It's worth noting that these others exist, but also worth noting that none of them are anything close to mainstream. NewtonScript seemed to have some traction for a while, but died with its platform. It's also possible to extend some modern languages in ways which add prototypal capabilities.

Java Object Oriented Concepts in Javascript

I've been at the same quest as you but I've had to scrape and fetch knowledge all over the place.

There are tons of excellent posts all over stackoverflow for all these subjects, and then there is MDN I would also recommend peering into the source of popular libraries like jquery. See this source viewer http://james.padolsey.com/jquery/

This is a BRILLIANT interactive tutorial by the great John Resig:

http://ejohn.org/apps/learn/

Here are some very good SO posts that helped me understand JS better:

How to "properly" create a custom object in JavaScript?

What is the 'new' keyword in JavaScript?

Why is JavaScript prototyping?
Why is it necessary to set the prototype constructor?

Call base method in Javascript using Douglas Crockford's functional inheritance

Help understanding jQuery's jQuery.fn.init Why is init in fn

What does jQuery.fn mean?

Why 'this' resolution is so special in JavaScript?

What is the difference between call and apply?

Dynamic function call (apply)

JavaScript data formatting/pretty printer

Checking if a key exists in a JavaScript object?

Here are some posts about the quirkyness of javascript and stuff you prolly didn't know:

Is it possible to reflect the arguments of a Javascript function?

function arguments

What is the !! (not not) operator in JavaScript?

How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?

Which equals operator (== vs ===) should be used in JavaScript comparisons?
Behavior of delete operator in javascript

var myArray =[], name;?

Why is null an object and what's the difference between null and undefined?

Checking for null/undefined in JavaScript

What does the exclamation mark do before the function?



Related Topics



Leave a reply



Submit