Home > Web Front-end > JS Tutorial > Is JavaScript a true OOP language?

Is JavaScript a true OOP language?

韦小宝
Release: 2017-11-30 10:22:26
Original
1845 people have browsed it

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);
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. 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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template