JS in prototype usage (GO)
The object-oriented features that JavaScript can implement are:
• Public attribute (common field)
• Public method
• Private property (privately field)
• Private method (privately field)
• Method Overloading (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)
Example One (the type of behavior that is allowed to be added in JavaScript): You can use Proptotype on a type to add behavior to a type. These behaviors can only be represented on instances of the type. The allowable types in JS are array, Boolean, Date, Enumerator, Error, Function, number, Object, REGEXP, String
JS Code
- <script type= "Text/javascript" >
- Object.prototype.Property = 1;
- Object.prototype.Method = function ()
- {
- Alert (1);
- }
- var obj = new Object ();
- Alert (obj. property);
- Obj. Method ();
- </script>
<script type= "text/javascript" >object.prototype.property = 1;object.prototype.method = function () {alert (1);} var obj = new Object (), alert (obj. property), obj. Method ();</script>
Example two (restrictions used by prototype): prototype cannot be used on an instance, or a compilation error occurs
JS Code
- <script type= "Text/javascript" >
- var obj = new Object ();
- Obj.prototype.Property = 1; Error
- Error
- Obj.prototype.Method = function()
- {
- Alert (1);
- }
- </script>
<script type= "Text/javascript" >var obj = new Object (); obj.prototype.Property = 1; Error//errorobj.prototype.method = function () {alert (1);} </script>
example three (how to define a static member on a type): You can define a "static" property and method for the type, which is called directly on the type
JS Code
- <script type= "Text/javascript" >
- Object.property = 1;
- Object.Method = function()
- {
- Alert (1);
- }
- alert (object.property);
- Object.Method ();
- </script>
<script type= "text/javascript" >object.property = 1;object. Method = function () {alert (1);} alert (object.property); Object. Method ();</script>
Example Five (): This example demonstrates the usual way to define a type in JavaScript
JS Code
- <script type= "Text/javascript" >
- function AClass ()
- {
- this. property = 1;
- this. Method = function()
- {
- Alert (1);
- }
- }
- var obj = new aclass ();
- Alert (obj. property);
- Obj. Method ();
- </script>
<script type= "Text/javascript" >function AClass () {this. property = 1;this. Method = function () {alert (1);}} var obj = new AClass (); alert (obj. property), obj. Method ();</script>
example Six (the type of behavior allowed in JavaScript): You can use prototype to add properties and methods to a custom type externally.
JS Code
- <script type= "Text/javascript" >
- 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 ();
- </script>
-
<script type= "Text/javascript" >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 ();</script>
Example Eight (): You can change properties on an object. (This is yes) you can also change the method on the object. (Different from the universal object-oriented concept)
JS Code
- <script type= "Text/javascript" >
- function aclass ()
- {
- this . property = 1;
- this . method = function ()
- {
- alert (1);
- }
- }
- var obj = < Strong>new aclass ();
- obj. property = 2;
- obj. method = function ()
- {
- alert (2);
- }
- alert (obj. property);
- obj. Method ();
- </script>
<script type= "Text/javascript" >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 ();</script>
Example Nine (): You can add properties or methods on an object
JS Code
- <script type= "Text/javascript" >
- function aclass ()
- {
- this . property = 1;
- this . method = function ()
- {
- alert (1);
- }
- }
- var obj = < Strong>new aclass ();
- obj. property = 2;
- obj. method = function ()
- {
- alert (2);
- }
- alert (obj. property);
- obj. Method ();
- </script>
-
<script type= "Text/javascript" >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 ();</script>
Example 10 (How to inherit one type from another): This example shows how a type inherits from another type.
JS Code
- <script type= "Text/javascript" >
- 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 ();
- </script>
<script type= "Text/javascript" >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 ();</script>
Example 11 (How to redefine a member of a parent class in a subclass): This example shows how subclasses can override the properties or methods of a parent class.
JS Code
- <script type= "Text/javascript" >
- 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 ();
- </script>
JS in prototype usage (GO)