


JavaScript object-oriented programming basics: encapsulation_js object-oriented
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 been regarded as a procedural language. It is difficult to complete complex functions." However (I want to say it bitterly and sadly), "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.
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 encapsulation. As we all know, objects are 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 function is new, 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 "abstract classes":
/*
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 with 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 that above "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 execution effect:
function test() {
var obj = new class1();
obj.oninit = function () { alert( " test " ); }
obj.oninit();
}

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



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

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.

JavaScript and WebSocket: Building an efficient real-time search engine Introduction: With the development of the Internet, users have higher and higher requirements for real-time search engines. When searching with traditional search engines, users need to click the search button to get results. This method cannot meet users' needs for real-time search results. Therefore, using JavaScript and WebSocket technology to implement real-time search engines has become a hot topic. This article will introduce in detail the use of JavaScript
