Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to know what is the core difference between

Object Oriented and Object based languages

I have read many post all of them are saying two things

  1. Object-oriented language supports all the features of OOPs and Object-based language doesn't support all the features of OOPs like Polymorphism and Inheritance.

  2. They are giving example of javascript as object based and java as object oriented

Like this post of stackoverflow

Difference between object oriented and object based language

But I want to know what is the core difference between both the concept regardless of any language.

Got the answer

Finally got the thing

thanks to Matías Fidemraizer

Answer which is not dependent on any language, not dependent on any feature, the core differnce for which I am loooking that is

The language which itself contains objects is called as object based language and the language with follows object oriented concepts is known as object oriented language

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

JavaScript is a prototype-oriented language.

It can build actual objects from a constructor function and it has almost any feature that any object could have:

  • Constructor.
  • Methods (i.e. functions in JavaScript).
  • Properties (since ECMA-Script 5, "getters/setters").
  • Instances.

In JavaScript, any object has a prototype, including functions. The prototype itself is a rudimentary way of adding object members to any newly created instance of the whole object.

var constructor = function() { };
constructor.prototype.text = "hello world";

alert(new constructor().text); // This alerts hello world

Why JavaScript isn't an object-oriented programming (scripting) language? Because it has no feature that fits the requirements of the definition of object-oriented programming:

  • Polymorphism: No. You can change the behavior of a prototype member, but this is just reusing the identifier. You aren't able to access the previous implementation of the member in a pseudo-derived object.
  • Inheritance: Not at all. Maybe prototype chain might be comparable to inheritance but JavaScript (ECMA-Script 5.x or earlier versions) has no syntax-based inheritance like other OOP-based languages (i.e. Java, C#, Ruby, Python, VisualBasic.NET, ...).
  • Encapsulation. Yes, of course, but there's no way to create actual private or internal object members.

Perhaps I forgot to mention some other detail, but I honestly believe that this is a good summary.

Update and summary

The core difference is an object-oriented programming language has the features that an object-oriented paradigm must have in order to be considered an object-oriented programming language. Thus, JavaScript, for now, isn't an actual object-oriented programming language because it lacks actual polymorphism and inheritance.

Update: Does ES2015 and above changed the situation?

Esthetically speaking yes, ES2015 and above has a major improvement that let consider a not fully but more closer to an object-oriented programming: syntactic sugar to call to the super class.

For example:

class A {
     doStuff() {
         console.log("hello world");
     }
}

class B extends A {
     doStuff() {
         super.doStuff();
         console.log("...and goodbye!");
     }
}

This is polymorphism. A more specialized class can override its base class to both completely change a function behavior or do what the base was already doing, adding new code to the function.

BTW, ES2015 and above still lacks true encapsulation: where are access modifiers like private or public here? Nowhere.

And, at the end of the day, ES2015 and above implement class-based OOP but it's still a syntactic sugar layer on top of ECMAScript 5.x... The above code still works with prototypes under the hoods and it works the same way as if you would code it in ECMAScript 5.x:

function A() {
}

A.prototype.doStuff = function() {
    console.log("hello world");
};

function B() {
}

B.prototype = Object.create(A.prototype);
B.prototype.doStuff = function() {
    A.prototype.doStuff.call(this);
    console.log("...and goodbye!");
};

Let's hope I'll need to update this answer again because ES2020 has already proposed access modifiers and we'll be able to consider JavaScript another language which fully-supports object-oriented programming!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...