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 features of types and objects, it is more important to look at how to write good and reusable types. After all, JavaScript, a popular scripting language that can be packaged well and form a large type library, makes sense for reuse.
Online for prototype a lot of articles, has not understood the core of the idea. Finally wrote a lot of examples of code to understand: prototype can only be used on the type.
Here are some examples of types and objects, and it may be easier to understand the relationship between types and objects after reading the example:
|
Example code
|
Description
|
1
|
O Bject.prototype.Property = 1; Object.prototype.Method = function ()
|
JS are array, Boolean, Date, Enumerator, Error, Function, number, Object, REGEXP, String |
2
|
V Ar obj = new Object (); Obj.prototype.Property = 1; //error
//error
Obj.prototype.Method = function ()
|
|
3
|
Object.property = 1;
|
|
4
|
Object.property = 1;
|
|
5
|
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
var obj = new AClass ();
Alert (obj. property);
Obj. Method ();
|
This example demonstrates the usual way 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 externally to add properties and methods to a custom type.
|
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 ();
|
The properties or methods of a custom type cannot be changed externally by prototype.
This example shows that the properties and methods invoked are still the result of the original definition.
|
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 the properties on an object. (This is for sure)
You can also change the method on the object. (unlike the common object-oriented concept)
|
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 a property or method to an object
|
10
|
This. method = function ()
This. METHOD2 = function ()
Aclass2.prototype = new AClass ();
var obj = new AClass2 ();
|
This example shows how a type inherits from another type.
|
11
|
This. method = function ()
This. METHOD2 = function ()
Aclass2.prototype = new AClass ();
AClass2.prototype.Property = 3;
AClass2.prototype.Method = function ()
var obj = new AClass2 ();
|
This example illustrates how subclasses can override the properties or methods of a parent class.
|
In the above example, the important things about reusing by type are:
• The type of behavior allowed to be added in example 1:javascript
• Examples of limitations used by 2:prototype
• Example 3: How to define a static member on a type
• Example 7:prototype restrictions on members of a redefined type
• Example 10: How to make one type inherit from another type
• Example 11: How to redefine the members of a parent class in a subclass
The object-oriented features that JavaScript can implement are:
• Public Property (field)
• Public methods (common method)
• Private Property (privacy field)
• Private method (in private field)
• Method overload (methods overload)
• Constructors (constructor)
• Events (Event)
• Single inheritance (Inherit)
• Subclasses override properties or methods of the parent class (override)
• Static properties or methods (static member)