Javascript override methods of parent classes

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Javascript override methods of parent classes

Postby nam.le » Wed Feb 22, 2023 1:46 pm

Dear all,
i face this serious problem and need your help.
I have define a class Parent with contructor method and extend it in Child class.
Now i want to override a method in the Parent class by calling it and add some thing more to it (see code below).
However, obviously there is no _super in Javascript. Could you please advice how to do it in servoy ?
Best regards and many thanks
Nam
Code: Select all
/**
* @public
* @constructor
* @this {Parent}
function Parent(){
   this.doSomething = function (){
      application.output('do something in parent class');
   }
}

/**
* @public
* @constructor
* @extends {Parent}
*/
function Child(){
   this.doSomething = function (){
      _super.doSomething();
      application.output('do some more things in child class');
   }
}

function test(){
   var child = new Child();
   child.doSomething();
}
nam.le
 
Posts: 22
Joined: Thu Jan 21, 2021 1:32 pm

Re: Javascript override methods of parent classes

Postby rvanderburg » Wed Feb 22, 2023 3:31 pm

hi i asked chatgpt for you :) hope that helps

I see what the problem is! There are a couple of syntax errors in your code that may be causing it to not work as expected:

You have an incomplete comment before the Parent constructor function. The comment is not closed properly, so it will cause a syntax error. To fix this, you can add the closing */ before the function Parent() line.

You are using _super.doSomething() in the Child constructor, but _super is not defined. You can fix this by calling the Parent constructor using the call method and passing this as the context. This will ensure that the Parent constructor is called with the correct this value.

Here's the corrected code:

javascript
Copy code
/**
* @public
* @constructor
* @this {Parent}
*/
function Parent() {
this.doSomething = function() {
application.output('do something in parent class');
};
}

/**
* @public
* @constructor
* @extends {Parent}
*/
function Child() {
Parent.call(this);
this.doSomething = function() {
Parent.prototype.doSomething.call(this);
application.output('do some more things in child class');
};
}

function test() {
var child = new Child();
child.doSomething();
}
With these changes, the Child object should call the Parent object's doSomething() method before executing its own.
rvanderburg
Site Admin
 
Posts: 78
Joined: Wed May 04, 2011 10:28 am

Re: Javascript override methods of parent classes

Postby sbutler » Wed Feb 22, 2023 6:39 pm

I'd suggest looking at some of the code for the open source Servoy modules, for example, svyUtils has some good patterns you could model after: https://github.com/Servoy/svyUtils/blob ... elUtils.js
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 16 guests