Table of Contents
What is OOP?
JavaScript and OOP
“Okay, but then again, JavaScript doesn’t have classes.”
Home Web Front-end JS Tutorial Is JavaScript an OOP language?

Is JavaScript an OOP language?

Oct 21, 2017 am 10:06 AM
javascript js language

I know, this topic has been discussed too many times. However, it is mentioned over and over again. Whenever developers of Java or C# or any other OOP language come into contact with JavaScript, these developers complain a lot. They say that working with JavaScript is a complete mess. It has no types, is not well structured, is a bit weird, has poor object support, and is definitely not an OOP language.

Some of these complaints may be acceptable, but others are biased, such as the statement that JavaScript has no types and therefore is not an OOP language. Regarding the latter point, before jumping to conclusions, you should ask yourself: What makes a programming language an object-oriented programming language?

What is OOP?

There is no formal standard specification for the OOP pattern. There is no technical document that defines what OOP is and what is not. The OOP definition is mainly based on common sense in papers published by early researchers such as Kristen Nygaard, Alan Kays, William Cook and others. There have been many attempts to define OOP and a broadly accepted definition to classify programming languages, since object-oriented is based on two requirements:

  • The ability to model problems through objects.

  • Supports some principles that allow modularity and code reuse.

To meet the first requirement, the language must enable developers to use objects to describe reality and define relationships between objects, as follows:

  • Association: The ability of an object to reference another independent object.

  • Aggregation: The ability of an object to be embedded in one or more independent objects.

  • Composition: The ability of an object to embed one or more dependent objects.

Generally, the second requirement is met if the language supports the following principles:

  • Encapsulation: Focus on a single unit of data and manipulation code entity and the ability to hide its internal details.

  • Inheritance: The mechanism by which an object obtains some or all elements from one or more other objects.

  • Polymorphism: The ability to handle objects differently depending on their data type or structure.

Languages ​​that meet these requirements are usually classified as object-oriented.

JavaScript and OOP

So now we know what an OOP language should look like. So, can we prove that JavaScript is an OOP language? Let's try it.

We know that the ability of JavaScript objects to support association, aggregation and combination is not strong. Please look at the following code:

var johnSmith = {
 firstName: "John",
 lastName: "Smith",
 address: { //Composition
 street: "123 Duncannon Street",
 city: "London",
 country: "United Kingdom"
 }
};
var nickSmith = {
 firstName: "Nick",
 lastName: "Smith",
 address: { //Composition
 street: "321 Oxford Street",
 city: "London",
 country: "United Kingdom"
 }
};
johnSmith.parent = nickSmith; //Association
var company = {
 name: "ACME Inc.",
 employees: []
};
//Aggregation
company.employees.push(johnSmith);
company.employees.push(nickSmith);
Copy after login

In the above code, you can find an example of a combination (address attribute), an example of association (parent attribute) and an example of aggregation (employees attribute).

As for encapsulation, JavaScript objects are entities that support data and functions, but they do not have high-level native support to hide internal details. JavaScript objects don't care about privacy. All properties and methods are publicly accessible if you are not careful. However, we can apply several techniques to define the internal state of an object and protect it from external access: Exploit closures using getters and setters. JavaScript supports inheritance at a base level through so-called prototypal inheritance. Even though some developers think it's a bit simplistic, JavaScript's inheritance mechanism is completely valid and allows you to get the same results as most recognized OOP languages. Whatever you think, JavaScript has a mechanism through which "an object obtains some or all functionality from one or more other objects", and this is inheritance.

The challenge of polymorphism seems to be more difficult because many people associate this concept with data types. In fact, polymorphism touches many aspects of programming languages ​​and is not just related to OOP languages. Typically it involves items such as generics, overloading, and structural subtypes. All of this seems overwhelming for a "simple" and weakly typed language: JavaScript. But this is not the case: in JavaScript, we can achieve different types of polymorphism in several ways, and we may have done it many times without realizing it.

OOP without classes

“Okay, but then again, JavaScript doesn’t have classes.”

Many developers believe that JavaScript lacks the concept of classes and do not treat JavaScript as It is a true object-oriented language because it does not enforce OOP principles.

However, we can see that in the informal definition, there is no explicit mention of classes. It is true that objects require properties and principles. But classes are not really a requirement, they are just sometimes a convenient way to abstract a set of objects with common properties. Therefore, even if a language's supporting objects do not have classes, it can be an object-oriented language, such as JavaScript.

Additionally, OOP principles are intended to be supported. In order to program in a language, OOP principles should not be mandatory. A developer can choose to use constructs that allow him to create object-oriented code, or he can choose not to use them. Many people criticize JavaScript because developers can write code that violates OOP principles. But this is just a choice of programmer, not a limitation of the language. This happens with other programming languages ​​as well, like C++.

So, we can conclude that the lack of abstract classes and allowing developers to freely use or not use functions that support OOP principles is not the real obstacle to identifying JavaScript as an OOP language

The above is the detailed content of Is JavaScript an OOP language?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

3 Ways to Change Language on iPhone 3 Ways to Change Language on iPhone Feb 02, 2024 pm 04:12 PM

It's no secret that the iPhone is one of the most user-friendly electronic gadgets, and one of the reasons why is that it can be easily personalized to your liking. In Personalization, you can change the language to a different language than the one you selected when setting up your iPhone. If you're familiar with multiple languages, or your iPhone's language setting is wrong, you can change it as we explain below. How to Change the Language of iPhone [3 Methods] iOS allows users to freely switch the preferred language on iPhone to adapt to different needs. You can change the language of interaction with Siri to facilitate communication with the voice assistant. At the same time, when using the local keyboard, you can easily switch between multiple languages ​​to improve input efficiency.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to set the language of Win10 computer to Chinese? How to set the language of Win10 computer to Chinese? Jan 05, 2024 pm 06:51 PM

Sometimes we just install the computer system and find that the system is in English. In this case, we need to change the computer language to Chinese. So how to change the computer language to Chinese in the win10 system? Now Give you specific operation methods. How to change the computer language in win10 to Chinese 1. Turn on the computer and click the start button in the lower left corner. 2. Click the settings option on the left. 3. Select "Time and Language" on the page that opens. 4. After opening, click "Language" on the left. 5. Here you can set the computer language you want.

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Apr 11, 2024 pm 08:52 PM

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level

How to change the language display of vivox60pro vivox60pro system language setting method How to change the language display of vivox60pro vivox60pro system language setting method Mar 23, 2024 am 09:06 AM

1. Click [System Management] in the phone settings menu. 2. Click the [Language] option. 3. Select the system language you want to use.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Thoughts and practice on assisted generation of B-end front-end code under large models Thoughts and practice on assisted generation of B-end front-end code under large models Apr 18, 2024 am 09:30 AM

1. Code specifications during background reconstruction work: During the B-end front-end development process, developers will always face the pain point of repeated development. The element modules of many CRUD pages are basically similar, but they still need to be developed manually, and time is spent on simple element construction. This reduces the development efficiency of business requirements. At the same time, because the coding styles of different developers are inconsistent, it makes it more expensive for others to get started during iterations. AI replaces simple brainpower: With the continuous development of large AI models, it has simple understanding capabilities and can convert language into instructions. General instructions for building basic pages can meet the needs of daily basic page building and improve the efficiency of business development in general scenarios. 2. Generate link list. B-side page lists, forms, and details can all be generated. Links can be roughly divided into the following categories:

See all articles