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:
- Function B (arg1, arg2) // assume that subclass B has two initial values, in which arg1 is passed to parent Class
- {
- // Inherit
- A. Call (this, arg1); // assume that parent class A has an initial value.
- }
I recently found some problems with function inheritance,
See the following:
Suppose:
- Function A (arg1)
- {
- This. M1 = arg1;
- This. ocap = Document. getelementbyid (arg1 );
- This. ocap. onmousedown = This. capturemouse (this );
- }
- // Define a member function externally
- A. Prototype. capturemouse = function (othis)
- {
- Return function ()
- {
- Othis. ocap. setcapture (true );
- }
- }
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:
- Function A (arg1)
- {
- This. M1 = arg1;
- This. ocap = Document. getelementbyid (arg1 );
- // Define the capturemouse function first
- This. capturemouse = function (othis)
- {
- Return function ()
- {
- Othis. ocap. setcapture (true );
- }
- }
- // Apply capturemouse again
- This. ocap. onmousedown = This. capturemouse (this );
- }