Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is JavaScript OOP language?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "JavaScript is an OOP language?" Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "JavaScript is an OOP language?"

What is OOP?

There is no formal standard specification for OOP patterns. There is no technical document that defines what is and is not OOP. The definition of OOP is largely based on common sense in papers published by earlier researchers such as Kristen Nygaard, Alan Kays, William Cook, et al. There have been many attempts to define OOP and a widely accepted definition to classify programming languages because object orientation is based on two requirements:

Ability to model problems through objects.

Supports principles that permit modularity and code reuse.

To meet these requirements, 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 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 data and manipulate a single entity of code and hide its internal details.

Inheritance: The mechanism by which an object acquires some or all features from one or more other objects.

Polymorphism: The ability to process objects differently depending on data type or structure.

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

JavaScript and OOP

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

We know that JavaScript objects are not strong enough to support association, aggregation, and composition. Please see 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 code above, you can find an example of a combination (address attribute), an example of an association (parent attribute), and an example of an aggregation (employees attribute).

As for encapsulation, JavaScript objects are entities that support data and functions, but they don't have advanced native support to hide internal details. JavaScript objects don't care about privacy. All properties and methods are publicly accessible if caution is not taken. However, we can apply several techniques to define the internal state of an object and protect it from external access: using getters and setters to leverage closures.

JavaScript supports inheritance at the base level through so-called prototypical inheritance. Even if some developers think it's a bit simplistic, JavaScript's inheritance mechanism is fully effective and allows you to get the same results as most recognized OOP languages. Whatever you think, JavaScript has a mechanism by which "an object acquires some or all of its functionality from one or more other objects." This is inheritance.

The challenge of having polymorphism seems more difficult because many people associate this concept with data types. In fact, polymorphism involves many aspects of programming languages, and not just OOP languages. Typically it involves items such as generics, overloading, and structural subtypes. All this seems overwhelming for a "simple" and weakly typed language-JavaScript. However, this is not the case: in JavaScript, we can implement different types of polymorphism in several ways, and perhaps we have done it many times before we know it.

OOP without classes

"Okay, but then again, JavaScript doesn't have classes. "

Many developers believe JavaScript lacks the concept of classes and fail to see JavaScript as a true object-oriented language because it does not enforce compliance with OOP principles.

However, we can see that classes are not explicitly mentioned in the informal definition. Admittedly, objects require properties and principles. But classes aren't really a requirement; they're just sometimes a convenient way to abstract a set of objects with common attributes. Thus, even if a language has no classes for its supporting objects, it can be an object-oriented language, such as JavaScript.

Furthermore, the purpose of the OOP Principles is to gain support. 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 a programmer's choice, not a language limitation. This happens with other programming languages, such as C++.

So, we can conclude that the lack of abstract classes and allowing developers to freely use or not use features that support OOP principles is not the assumption that JavaScript is a real obstacle to the OOP language.

At this point, I believe that everyone has a deeper understanding of "JavaScript is OOP language?", you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report