JavaScript object-oriented programming basics: encapsulation
For a long time (I want to gloat here), js has been "a kind of embellishment, completing very limited functions, such as form validation, and its language itself has always been regarded as a procedural language. It is difficult to complete complex functions." However (I want to say this with bitterness and sorrow), "The emergence of Ajax has made complex scripts a necessary component, which has put forward new requirements for JavaScript programming. Many Ajax applications have begun to use JavaScript object-oriented Properties are developed to make the logic clearer. In fact, JavaScript provides a complete mechanism to implement object-oriented development ideas.” Oh my God, I didn’t want to learn and didn’t dare to learn, but now I have to bite the bullet and learn.
There is so much nonsense about objects here. We all know that the three main characteristics of object-oriented programming are: encapsulation, inheritance and polymorphism. Below, we will record some learning experiences around these three characteristics.
Okay, let’s start with the introduction of encapsulation. As we all know, the object is the most basic unit of encapsulation. Encapsulation prevents the impact of changes caused by program interdependencies. Object-oriented encapsulation is clearer and more powerful than traditional language encapsulation. Code is cheap. Let’s look at a simple code:
// Define a class by defining a function
function class1() {
// Definition of class members and constructor
// Here class1 is both a function and a class. As a function, it can be understood as the constructor of a class, responsible for initialization.
}
// Use the new operator to obtain an instance of a class
var obj = new class1();
/* Putting aside the concept of class, from the form of the code, class1 is a function, so can all functions be operated with new? The answer is yes.
In JavaScript, functions and classes are the same concept. When a new function is created, an object will be returned. If there are no initialized class members in this function, an empty object will be returned.
In fact, when new a function, this function is the constructor of the represented class, and all the code in it can be regarded as working to initialize an object. Functions used to represent classes are also called constructors.
In JavaScript, each object can be viewed as a collection of multiple properties (methods)
*/
function test() {
alert( typeof (obj));
}
The above code defines a class class1, which is a simple encapsulation in js. Let's see how js defines a "static class",
function class1() { // Constructor
}
// Static property
class1.staticProperty = " test " ;
// Static method
class1.staticMethod = function () {
alert(class1.staticProperty);
}
function test() {
// Call the static method
class1.staticMethod();
alert( typeof ( class1));
}
Then look at the "abstract class":
/*
In traditional object-oriented languages, virtual methods in abstract classes must first is declared, but can be called from other methods.
In JavaScript, virtual methods can be seen as methods that are not defined in the class, but have been used through this pointer.
Different from traditional object-oriented methods, virtual methods here do not need to be declared but are used directly. These methods will be implemented in the derived class
*/
// Define the extend method
Object.extend = function (destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
}
Object.prototype.extend = function (object) {
return Object.extend. apply( this , [ this , object]);
}
// Define an abstract base class base, no constructor
function base() { }
base.prototype = {
initialize: function () {
this .oninit(); // Called a virtual method
}
}
// Define class1
function class1() {
// Constructor
}
// Let class1 inherit from base and implement the oninit method
class1.prototype = (new base()).extend({
oninit: function () { // Implement the oninit virtual method in the abstract base class
// Implementation of oninit function
}
});
We see above that "let class1 inherit from base and implement oninit in it" Method", the concept of "inheritance" is used, please pay attention. Let’s take a look at the effect of execution:
function test() {
var obj = new class1();
obj.oninit = function () { alert( " test " ); }
obj.oninit();
}
For more javascript object-oriented programming basics: encapsulation related articles, please pay attention to 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



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.
