Javascript is object-based and any element can be viewed as an object. However, the types and objects are different. In this article, in addition to discussing some characteristics of types and objects, we are more important to study how to write a good and reusable type. After all, JavaScript, a popular scripting language, is very meaningful for reuse if it can be well encapsulated and form a large type library. There are a lot of articles on prototype on the internet, and I have never understood the core idea. After writing a lot of sample code, you can understand that prototype can only be used for type. The following are some examples of types and objects. After reading the examples, you may better understand the relationship between types and objects:
| |
Sample Code |
Description |
| 1 |
Object. Prototype. Property = 1; Object. Prototype. method = function () {alert (1);} var OBJ = new object (); alert (obj. Property); obj. Method (); |
You can use proptotype to add behavior for the type. These actions can only be reflected on instances of the type. Supported types in JS include array, Boolean, date, enumerator, error, function, number, object, Regexp, string |
| 2 |
VaR OBJ = new object (); OBJ. Prototype. Property = 1; // error // errobj. Prototype. method = function () {alert (1 );} |
Prototype cannot be used on instances; otherwise, a compilation error occurs. |
| 3 |
Object. Property = 1; object. method = function () {alert (1) ;}alert (object. Property); object. Method (); |
You can define "static" attributes and methods for the type and call them directly on the type. |
| 4 |
Object. property = 1; object. method = function () {alert (1);} var OBJ = new object (); alert (obj. property); // errorobj. method (); // Error |
The instance cannot call static properties or methods of the type. Otherwise, an error of undefined object occurs. |
| 5 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} var OBJ = new Aclass (); alert (obj. property); obj. method (); |
This example demonstrates how to define a type in JavaScript. |
| 6 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} Aclass. prototype. property2 = 2; Aclass. prototype. method2 = function {alert (2);} var OBJ = new Aclass (); alert (obj. property2); obj. method2 (); |
You can use prototype to add attributes and methods for custom types externally. |
| 7 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} Aclass. prototype. property = 2; Aclass. prototype. method = function {alert (2);} var OBJ = new Aclass (); alert (obj. property); obj. method (); |
You cannot use prototype to change attributes or methods of a custom type externally. In this example, we can see that the properties and methods of the call are still defined at the beginning. |
| 8 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} var OBJ = new Aclass (); obj. property = 2; obj. method = function () {alert (2);} alert (obj. property); obj. method (); |
You can change attributes on an object. (This is for sure) You can also change the method on the object. (Unlike general object-oriented concepts) |
| 9 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} var OBJ = new Aclass (); obj. property2 = 2; obj. method2 = function () {alert (2);} alert (obj. property2); obj. method2 (); |
You can add attributes or methods to an object. |
| 10 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} function aclass2 () {This. property2 = 2; this. method2 = function () {alert (2) ;}} aclass2.prototype = new Aclass (); var OBJ = new aclass2 (); alert (obj. property); obj. method (); alert (obj. property2); obj. method2 (); |
This example illustrates how a type inherits from another type. |
| 11 |
Function Aclass () {This. property = 1; this. method = function () {alert (1) ;}} function aclass2 () {This. property2 = 2; this. method2 = 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 to override the attributes or methods of the parent class. |
In the above example, the important aspects of reuse through type implementation are: · Example 1: the type that allows adding behavior in Javascript · Example 2: Restrictions on the Use of prototype · Example 3: how to define a static member of a type-Example 7: restrictions on a prototype member of a type-Example 10: How to Make a type inherit from another type-example 11: how to redefine the members of the parent class in the subclass. Visible to Javascript, object-oriented features can be implemented by: · public field · Public Method) · private field · method overload · constructor · event · single inherit) · override attributes or methods (override) of the parent class by subclass · static attributes or methods (static member)