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!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools