JavaScript is object-based, any element can be seen. However, types and objects are different. In this article, in addition to discussing some of the types and objects, we are more important to study how to write well and beneficial to reuse. After all, JavaScript This popular scripting language can make a good package and form a huge type of library, which is very meaningful for reuse.
There are many articles on Prototype on the Internet and have not understood the idea of the core. Finally, I wrote a lot of examples of code: Prototype can only be used on the type.
Here are some examples about types and objects, you may be more easier to understand the types and objects after reading the example:
Example code
Description
1
Object.Prototype.property = 1;
Object.prototype.method = function ()
{
Alert (1);
}
Var obj = new object ();
Alert (Obj.property);
Obj.method ();
You can add behavior to the type using ProPtotype on the type. These behaviors can only be embodied in an instance of the type.
The types allowed in JS include Array, Boolean, Date, Enumerator, Error, Function, Number, Object, Regexp, String
2
Var obj = new object ();
Obj.Prototype.property = 1; // Error
// Error
Obj.Prototype.Method = function ()
{
Alert (1);
}
You can't use prototype on an instance, otherwise compiling errors
3
Object.property = 1;
Object.method = function ()
{
Alert (1);
}
Alert (Object.property);
Object.method ();
You can define the "static" properties and methods for the type, you can call directly to the type.
4
Object.property = 1;
Object.method = function ()
{
Alert (1);
}
Var obj = new object ();
Alert (Obj.property); // Error
Obj.method (); // error
The instance cannot invoke the type of static attribute or method, otherwise an error in which an object is not defined.
5
Function ACLASS ()
{
this.property = 1;
this.method = function ()
{
Alert (1);
}
}
Var obj = new aclass ();
Alert (Obj.property);
Obj.method ();
This example demonstrates usually defined a type of method 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 ();
Properties and methods can be added to the externally using prototype for custom types.
Seduce
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 the custom type cannot be changed through Prototype.
This example can be seen that the attributes and methods of the call 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 ();
The property can be changed on the object. (This is affirmative)
It is also possible to change the method on the object. (Different from the universal 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 ();
Can increase attributes or methods on objects
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 is inherited 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 illustrates how the subclasses rewrite the properties or methods of the parent class.
In the above example, it is important to achieve reuse through types.
· Example 1: Types that allowed to add behavior in JavaScript
· Example 2: Restrictions used by prototype
· Example 3: How to define static members on the type
· Example 7: Prototype restriction on the member of the redefined type · Example 10: How to let a type inherit in another type
· Example 11: How to redefine the members of the parent class in the subclass
Combined with my previous article "How the JavaScript implements the object-oriented various features", it can be seen that the object-oriented features that JavaScript enabled:
· Public Field
· Public Method
· Private property (private field)
· Private field)
· Method Overload
· Constructor (Constructor)
· Event (Event)
· Single Inherit
· Subclass rewriting the properties or method of the parent class (OVERRIDE)
· Static property or method (Static Member)
OK! Next time I will summarize an article on how to write an object-oriented JavaScript code to everyone. Please ask everyone to make extensive opinions.