Is JavaScript a true OOP language?
JavaScriptObject-oriented Still not object-oriented, some people say that JavaScript is a mess, with no types, unreasonable structure, some weirdness, and poor object support. It is absolutely Not an OOP language. Is JavaScript really not an oop language? We together look!
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 orientation is based on two requirements:
The ability to model problems through objects.
Supports some principles that allow modularization 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: An object refers to another Independent object capabilities.
Aggregation: The ability of an object to embed 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: The ability to focus on a single entity of data and manipulation code, and 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 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 JavaScript objects's ability 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; //Associationvar company = { name: "ACME Inc.", employees: [] };//Aggregationcompany.employees.push(johnSmith); company.employees.push(nickSmith);
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. If you are not careful, all properties and methods will be publicly accessible. 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 Classes."
Many developers believe that JavaScript lacks the concept of classes and do not consider JavaScript a true object-oriented language because it does not enforce compliance with 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.
Furthermore, 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 programmer's choice, not a language limitation. This happens with other programming languages as well, like C++.
So, we can conclude that the lack of abstract classes and allowing developers the freedom to use or not use features that support OOP principles is not a real obstacle to identifying JavaScript as an OOP language.
After reading this article, you have new understandings and gains. Please help your classmates!
related suggestion:
Basic syntax of JavaScript study notes
How to use JavaScript to modify the pseudo-class style
Detailed explanation of the use of eval in JavaScript
The above is the detailed content of Is JavaScript a true OOP language?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

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

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

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

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

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

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
