Object-oriented JScript

xiaoxiao2021-03-05  33

Object-oriented JScript

In the client application of the web page, JS has become an indispensable part. Traditionally, the application of JS is completely based on process model, in which the usage of ordinary statements and global functions is most common. When the number of code is gradually increased, the maintenance of the entire project has become difficult, and the logic has gradually exceeded the control of the designer. At this time, we need to borrow software engineering to manage the project. The foundation of modern software engineering is a component-based, objective programming, and the program design process of UML design guidance is in an orderly manner. What is distressed is that when the concept of modern software engineering is infiltrated into the Web project, it has encountered a lot of problems, and there is almost no way to play its power.

What is the root of the problem? It is not an effective way to organize the JS program so that it can follow some basic object-oriented ideas. However, JS is not a way to reflect these ideas. This article tries to use certain special organization to make JS meet the basic object-oriented characteristics, and put a pad for further application models of software engineering.

Some classes are built in JScript, such as String, Array, Math, etc., users can directly vary from these classes and use their properties and methods. With this, JS can't say the characteristics of object-oriented language. An object-oriented language should have basic characteristics such as packaging, inheritance, polymorphism. JScript does not directly provide methods of implementing these features, but not completely unable.

First, the package in JS

An object-oriented language should allow the user to create a custom type, this JS is done, but its custom type is not qualified with a class, but as function, this function is equivalent to the constructor in other languages. In the custom type, the user can add an attribute and method. However, JS is not explicitly available to public, private, protected, etc., and no status defined by STATIC is provided. The following examples are creating a custom type and implementing a variety of defined methods one by one.

Example 1-1: This example demonstrates the process of creating and using a custom type containing properties and methods. In this example, we created a class called MyClass, which contains a name property and a showname method, then instantiates this class, that is, the method can be called, using the properties. Note that the methods and properties defined in the class should be bound to this class using the THIS keyword, and the binding name is used to access, not by the actual name.