Question
· Aug 31, 2023

Problem with overriding a javascript method

I have created 3 pages, the scripts are there:

Class BZ.Test1 Extends %ZEN.Component.page
{
    ClientMethod onloadHandler() [ Language = javascript ]
    {
        console.log("Test1");
    }
}

Class BZ.Test2 Extends BZ.Test1
{
    ClientMethod onloadHandler() [ Language = javascript ]
    {
        this.invokeSuper('onloadHandler',arguments);
        console.log("Test2");
    }
}

Class BZ.Test3 Extends BZ.Test2
{
    ClientMethod onloadHandler() [ Language = javascript ]
    {
        this.invokeSuper('onloadHandler',arguments);
        console.log("Test3");
    }
}

each class extends the previous class, and in method onloadHandler output something in the console. so i open the page Test3, and get the right result:

test1

test2

test3

 

but if I remove the method onloadHander from class Test3 then I get the following output:

test1

test2

test2

 

The code from Test2 will run 2 times. Even worse was: if I define more page class like the rule without rewrite of onloadHandler, e.g.: Test4  extends Test3 and Test5 extends Test4, then I get on the last page even more output of "Test2".

This result violates the overriding principle in object-oriented programming, such problem is available on all javascript method, in ZenMethod it works no problem.

 

Product version: IRIS 2020.1
$ZV: InterSystems Studio Client 2020.1.0 Build 215 Server IRIS for Windows (x86-64) 2020.1.4 (Build 536U)
Discussion (2)1
Log in or sign up to continue

Assuming that's a typo and the last one is supposed to be Class BZ.Test3, this is all correct and as expected. Since it extends Test2, Test3's onloadHandler function is identical to Test2's onloadHandler, which runs its superclass's onloadHandler and prints test2. Since its superclass is Test2, it's superclass's onloadHandler is also running its onloadHandler, then printing test2. Since Test2's superclass is Test1, Test2 is running its superclass's onloadHandler, printing test1.

Remember, removing the onloadHandler method from Test3 doesn't mean that it doesn't have an onloadHandler; it just means its onloadHandler is the same as the one in its superclass, Test2.

I can understand your theory, but why the same doesn't work like this in zenMethod?
When I change the code to the following

Class BZ.Test1 Extends %ZEN.Component.page
{
    ClassMethod DoIt() [ ZenMethod ]
    {
        Write "Test1",!
    }
}

Class BZ.Test2 Extends BZ.Test1
{
    ClassMethod DoIt() [ ZenMethod ]
    {
        Do ##Super()
        Write "Test2",!
    }
}

Class BZ.Test3 Extends BZ.Test2
{
}

When I run "do ##class(BZ.Test3).DoIt()" I get only

Test1

Test2

Here "Test2" is not output twice.