The object-oriented features that JavaScript can implement are: • Public properties (public field) • Publicly available method • Private attribute (private field) • Method overload ( Method overload) • Constructor (constructor) • Event • Single-Inherit • Subclass overrides the parent class's property or method (override) • Static property or method (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>
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>
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>
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 =&NBSP; function ()
- {
- alert (1);
- }
- }
- var obj =&NBSP; 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>
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 = 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 = 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>
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>
Prototype attribute interpretation of JS and its common methods