Brief analysis of types and objects in JavaScript

  • 2020-03-30 00:01:40
  • OfStack

JavaScript is object-based, and any element can be viewed as an object. However, types and objects are different. In this article, in addition to discussing some of the characteristics of types and objects, it is important to explore how to write good and reusable types. After all, a popular scripting language like JavaScript makes a lot of sense for reuse if it is well encapsulated and forms a large type library.

There are many articles about prototype on the Internet, and I have not understood the core idea. I ended up writing a lot of example code before I realized that prototype can only be used for types.

Here are some examples of types and objects that you might find easier to understand:

  The example code instructions 1 Object. The prototype. The Property = 1;
Object. The prototype. The Method = function () {       Alert (1); }   Var obj = new Object(); Alert (obj. Property); Obj. Method (); Proptotype can be used on a type to add behavior to the type. These behaviors can only be expressed on an instance of a type.

The types allowed in JS are Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String 2 Var obj = new Object();
Obj. Prototype. Property = 1;   / / the Error / / the Error Obj. Prototype. Method = function () {       Alert (1); } You cannot use prototype on an instance, otherwise a compilation error occurs 3 Object. The Property = 1; Object. The Method = function () {       Alert (1); }   Alert (Object. The Property); Object. The Method (); You can define "static" properties and methods for a type, calling them directly on the type 4 Object. The Property = 1; Object. The Method = function () {       Alert (1); } Var obj = new Object(); Alert (obj. Property);   / / the Error Obj. Method ();   / / the Error An instance cannot invoke a static property or method of a type, otherwise an error occurs that the object is not defined. 5 The function Aclass () { This. The Property = 1; This Method = function () {       Alert (1); } } Var obj is equal to new Aclass(); Alert (obj. Property); Obj. Method (); This example demonstrates the common way to define a type in JavaScript 6 The function Aclass () { This. The Property = 1; This Method = function () {       Alert (1); } } Aclass. Prototype. Property2 = 2; Aclass. Prototype. An = function {       Alert (2); } Var obj is equal to new Aclass(); Alert (obj. Property2); Obj. An (); You can use prototype externally to add properties and methods to your custom types. 7 The function Aclass () { This. The Property = 1; This Method = function () {       Alert (1); } } Aclass. Prototype. Property = 2; Aclass. Prototype. Method = function {       Alert (2); } Var obj is equal to new Aclass(); Alert (obj. Property); Obj. Method (); You cannot change the properties or methods of a custom type externally through prototype. The example shows that the properties and methods invoked are still the result of the original definition. 8 The function Aclass () { This. The Property = 1; This Method = function () {       Alert (1); } } Var obj is equal to new Aclass(); Obj. Property = 2; Obj. Method = function () {       Alert (2); } Alert (obj. Property); Obj. Method (); You can change properties on an object. (that's for sure) You can also change methods on objects. (contrary to popular object-oriented concepts) 9 The function Aclass () { This. The Property = 1; This Method = function () {       Alert (1); } } Var obj is equal to new Aclass(); Obj. Property2 = 2; Obj. An = function () {       Alert (2); } Alert (obj. Property2); Obj. An (); You can add properties or methods to an object 10 The function AClass () {             This. The Property = 1;             This Method = function ()             {                           Alert (1);             } }   The function AClass2 () {             Enclosing Property2 = 2;             Enclosing an = function ()             {                           Alert (2);             } } AClass2. Prototype = new AClass ();   Var obj = new AClass2(); Alert (obj. Property); Obj. Method (); Alert (obj. Property2); Obj. An (); This example illustrates how one type can inherit from another. 11 The function AClass () {             This. The Property = 1;             This Method = function ()             {                           Alert (1);             } }   The function AClass2 () {             Enclosing Property2 = 2;             Enclosing an = function ()             {                           Alert (2);             } } AClass2. Prototype = new AClass (); AClass2. Prototype. Property = 3; AClass2. Prototype. Method = function () {             Alert (4); } Var obj = new AClass2(); Alert (obj. Property); Obj. Method (); This example shows how a subclass overrides a parent class's properties or methods.

In the above example, it is important to:
, example 1: the type of behavior allowed to be added in JavaScript
, example 2: restrictions used by prototype
, example 3: how to define a static member on a type
, example 7: prototype's restrictions on redefining type members
, example 10: how to make a type inherit from another type
, example 11: how do I redefine a member of a parent class in a subclass

The object-oriented features that JavaScript can achieve are:
, public field
, public Method
, private field
, private field
, method overload
, constructor
・ events (event)
, single inherit
, subclasses override superclass properties or methods (override)
, static attribute or method (static member)

Related articles: