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