2

I read that one of the differences between Object-Based and Object-Oriented is that the former supports Built-in objects(eg., window object in Javascript).So, what exactly is a built-in object and why it isn't there in object-oriented language like java.

6
  • They're not particularly different from static variables. Commented Jul 25, 2017 at 16:23
  • 4
    Where'd you read that? It doesn't make a lot of sense--JS doesn't have a "built-in window object", the environment in which it runs may provide one. E.g., there's no window in NodeJS. Commented Jul 25, 2017 at 16:23
  • This distinction makes absolutely no sense. Java, or a Java-like language, may just as well provide built-in objects. Commented Jul 25, 2017 at 16:29
  • Btw, one could categorise Javascript as an object-oriented language and Java a class-oriented one. But that's a separate question. Commented Jul 25, 2017 at 16:30
  • 1
    Objects provided by the language or ecosystem. Commented Jul 26, 2017 at 10:34

3 Answers 3

2

That's not actually the difference between those two terms.

For a programming language to be considered "Object-Oriented", it must support the following four programming concepts:

  1. Inheritance
  2. Encapsulation
  3. Abstraction
  4. Polymorphism

There are many languages that support these "four pillars of OOP" (Java, C/C++, C#, JavaScript, etc.).

Some languages, however, don't. A famous example of that would be what we now call "Classic VB" (Visual Basic prior to the introduction of .NET). That language could simulate inheritance, but there was not actual mechanism built into the language for it, so while "Classic VB" had native objects, it is an "object-based", "not object-oriented", programming language because it does support the concept of objects, but not all the aspects that a true OOP language requires.

It should be noted that many OOP languages are built on the concept of "classes" as the mechanism to generate objects from. And, while this is a very popular way to architect objects, it is not a requirement for a language to be OO. JavaScript does not have classes (despite having a class keyword), it has "prototypes" and they are the architecture upon which objects are implemented.

Your question about "native" objects is not related to any of this. You most likely read that native objects were related to all of this at this Wikipedia page, but that page had many errors on it and I have edited that page to be more accurate. Whether or not a language has "built-in" or "native" objects is not at all related to whether it is object-oriented or object-based, since both types of languages are object-centric (my own term). For example, VB 6 was an object-based language, but supported a wide array of native objects and VB .NET (its successor) is object-oriented and also supports a vast amount of native objects.

I will tell you that a "native" object is simply one that is built right into the language specification itself and the runtime environment has access to it internally. In JavaScript, some examples would be String, Date, Array, RegEx, Math, Object, etc. Note that while, in your question, you mentioned window, window is not a native JavaScript object, that object is supplied by the browser that hosts the JavaScript runtime. If you were running your JavaScript in Node.js, window would not be available because it is not native to JavaScript and Node doesn't provide such an object to the runtime.

Here are some good links to look at to understand OOP concepts and how they work in JavaScript:

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't Javascript Object-based language? And object based language need not support Inheritance. en.wikipedia.org/wiki/Object-based_language
@sanjeeda It's not that an object-based language "need not" support inheritance. Object based languages "do not" support the all four of the "pillars of OOP" (abstraction, encapsulation, polymorphism, inheritance) - - JavaScript natively supports all of these.
0

The window object is the global object. It neither exists in Java nor JavaScript for Node.js because it refers to the browser window.

Read about all global objects here: Global Objects (MDN)

As you can see, there are lots of global objects. The Array global object, for instance, exists in both Java and JavaScript.

Comments

0

There are 'built in' objects in OO languages like Java (or most people would consider them as such), just think of the base Object (top of the inheritance hierarchy) and many of the most core features of the stuff that is in the standard library.

My impression is that people would be saying that to point out that there are still some objects in JavaScript, however you can't actually declare/create a full featured class like in other languages. But, I mean the array in C# and Java is just as much an object as the one in JavaScript.

In order for someone to refer to a language as being 'Object Oriented' I would assume it needs classes (or a similar construct, struct in Go), which is why you generally wouldn't describe JS as object oriented. That said, classes have been added to ES6 so it's arguably fully object oriented now, just with a weak type system.

12 Comments

" for someone to refer to a language as being 'Object Oriented' I would assume it needs classes " Not at all. See my answer for what constitutes an Object-Oriented Programming language. Classes are not, by any means a requirement. JavaScript doesn't have classes, it has prototypes and is object-oriented.
@ScottMarcus yes, I read your answer. It is far more technically accurate than this. Though I would say even based on your criteria classifying JS as OO is still a fairly open argument. It doesn't really have encapsulation and probably won't anytime soon because the scoping is so loose.
Huh? Of course JavaScript has encapsulation. The ability to have private data in an object is encapsulation and, of course JavaScript has this. The fact that scope is extremely flexible in JavaScript, doesn't mean it doesn't exist.
@ScottMarcus what constitutes private? I may be wrong but my impression would be the standard of 'private' isn't the same. It's like pseudo-encaspulation. Can you really prevent access to a field? Can you stop me from modifying the objects structures/definition?
@ScottMarcus that's not a property on a class, it's a local var hidden inside a closure/method. You can't attach private fields to the class, therefor it does not have encapsulation, at least not by the definition used for all the traditional OO languages you mentioned.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.