Prototype attribute interpretation of JS and its common methods

Source: Internet
Author: User

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
    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>

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>

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>

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 =&NBSP; function ()
    6. {
    7. alert (1);
    8. }
    9. }
    10. var  obj =&NBSP; new  aclass ();
    11. alert (obj. property);
    12. obj. Method ();
    13. </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>

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 = new aclass ();
  11. Obj. Property = 2;
  12. Obj. Method = function()
  13. {
  14. Alert (2);
  15. }
  16. Alert (obj. property);
  17. Obj. Method ();
  18. </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 = new aclass ();
  11. Obj. Property = 2;
  12. Obj. Method = function()
  13. {
  14. Alert (2);
  15. }
  16. Alert (obj. property);
  17. Obj. Method ();
  18. </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>

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>

Prototype attribute interpretation of JS and its common methods

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.