JS in prototype usage (GO)

Source: Internet
Author: User

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

    1. <script type= "Text/javascript" >
    2. Object.prototype.Property = 1;
    3. Object.prototype.Method = function ()
    4. {
    5. Alert (1);
    6. }
    7. var obj = new Object ();
    8. Alert (obj. property);
    9. Obj. Method ();
    10. </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

    1. <script type= "Text/javascript" >
    2. var obj = new Object ();
    3. Obj.prototype.Property = 1; Error
    4. Error
    5. Obj.prototype.Method = function()
    6. {
    7. Alert (1);
    8. }
    9. </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

    1. <script type= "Text/javascript" >
    2. Object.property = 1;
    3. Object.Method = function()
    4. {
    5. Alert (1);
    6. }
    7. alert (object.property);
    8. Object.Method ();
    9. </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

    1. <script type= "Text/javascript" >
    2. function AClass ()
    3. {
    4. this. property = 1;
    5. this. Method = function()
    6. {
    7. Alert (1);
    8. }
    9. }
    10. var obj = new aclass ();
    11. Alert (obj. property);
    12. Obj. Method ();
    13. </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

    1. <script type= "Text/javascript" >   
    2. function  aclass ()   
    3. {  
    4. this . property = 1;   
    5. this . method =  function ()   
    6. {  
    7.      alert (1);   
    8. }   
    9. }   
    10. aclass.prototype.property2 = 2;   
    11. aclass.prototype.method2 =  function   
    12. {   
    13.     alert (2);   
    14. }   
    15. var  obj =  new  aclass ();   
    16. alert (obj. Property2);   
    17. obj. METHOD2 ();   
    18. </script>   
    19.       

<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

    1. <script type= "Text/javascript" >   
    2. function  aclass ()   
    3. {  
    4. this . property = 1;   
    5. this . method =  function ()   
    6. {  
    7.      alert (1);   
    8. }   
    9. }   
    10. var  obj = < Strong>new  aclass ();   
    11. obj. property = 2;   
    12. obj. method =  function ()   
    13. {  
    14.      alert (2);   
    15. }   
    16. alert (obj. property);   
    17. obj. Method ();   
    18. </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

    1. <script type= "Text/javascript" >   
    2. function  aclass ()   
    3. {  
    4. this . property = 1;   
    5. this . method =  function ()   
    6. {  
    7.      alert (1);   
    8. }   
    9. }   
    10. var  obj = < Strong>new  aclass ();   
    11. obj. property = 2;   
    12. obj. method =  function ()   
    13. {  
    14.      alert (2);   
    15. }   
    16. alert (obj. property);   
    17. obj. Method ();   
    18. </script>   
    19.            

<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

  1. <script type= "Text/javascript" >
  2. function AClass ()
  3. {
  4. this. property = 1;
  5. this. Method = function()
  6. {
  7. Alert (1);
  8. }
  9. }
  10. function AClass2 ()
  11. {
  12. this. Property2 = 2;
  13. this. METHOD2 = function()
  14. {
  15. Alert (2);
  16. }
  17. }
  18. Aclass2.prototype = new aclass ();
  19. var obj = new AClass2 ();
  20. Alert (obj. property);
  21. Obj. Method ();
  22. Alert (obj. Property2);
  23. Obj. METHOD2 ();
  24. </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

      1. <script type= "Text/javascript" >
      2. function AClass ()
      3. {
      4. this. property = 1;
      5. this. Method = function()
      6. {
      7. Alert (1);
      8. }
      9. }
      10. function AClass2 ()
      11. {
      12. this. Property2 = 2;
      13. this. METHOD2 = function()
      14. {
      15. Alert (2);
      16. }
      17. }
      18. Aclass2.prototype = new aclass ();
      19. AClass2.prototype.Property = 3;
      20. AClass2.prototype.Method = function()
      21. {
      22. Alert (4);
      23. }
      24. var obj = new AClass2 ();
      25. Alert (obj. property);
      26. Obj. Method ();
      27. </script>

JS in prototype usage (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.