Some Problems in Javascript inheritance

Source: Internet
Author: User

The inheritance class of JavaScript has been said to be rotten. Its core is to run the parent class A () in subclass B ().

A concise implementation:

  1. Function B (arg1, arg2) // assume that subclass B has two initial values, in which arg1 is passed to parent Class
  2. {
  3. // Inherit
  4. A. Call (this, arg1); // assume that parent class A has an initial value.
  5. }

I recently found some problems with function inheritance,

See the following:

Suppose:

  1. Function A (arg1)
  2. {
  3. This. M1 = arg1;
  4. This. ocap = Document. getelementbyid (arg1 );
  5. This. ocap. onmousedown = This. capturemouse (this );
  6. }
  7. // Define a member function externally
  8. A. Prototype. capturemouse = function (othis)
  9. {
  10. Return function ()
  11. {
  12. Othis. ocap. setcapture (true );
  13. }
  14. }

At this time, a () itself runs well.

However, when B () inherits a, the interpreter cannot interpret capturemouse.

When running a in B (), the interpreter interprets a in sequence, so a. Prototype. capturemouse is not found.

After the code is changed to the following,

Solution:

  1. Function A (arg1)
  2. {
  3. This. M1 = arg1;
  4. This. ocap = Document. getelementbyid (arg1 );
  5. // Define the capturemouse function first
  6. This. capturemouse = function (othis)
  7. {
  8. Return function ()
  9. {
  10. Othis. ocap. setcapture (true );
  11. }
  12. }
  13. // Apply capturemouse again
  14. This. ocap. onmousedown = This. capturemouse (this );
  15. }

 

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.