How to call a parent function in an extended class?

peterraeves
Mega Guru

I am creating an extended class, and want to call the parent class's function. How would I do that. I tried the following and failed.

When I run this:

var a = Class.create();

a.prototype = {

      initialize: function() {

              gs.print('init a');

      },

      foo: function() {

              gs.print('foo a');

      },

      bar: function() {

              gs.print('bar a');

      },

      type: 'a'

};

var b = Class.create();

b.prototype = Object.extendsObject(a, {

      initialize: function() {

              //super();

              gs.print('init b');

      },

      foo: function() {

              gs.print('foo b');

              //super.foo();

      },

      type: 'b'

});

var c = new b();

c.foo();

c.bar();

I get this:

*** Script: init b

*** Script: foo b

*** Script: bar a

Which is as expected, but I would like to call the parent function twice. Once in the constructor and once in the function, though 'super' does not seem to work. Any ideas?

1 ACCEPTED SOLUTION

Okay, second conslusion. It seems that super is only available from ES6 and SNOW still uses ES5 (in Jakarta).



It seems I fixed the issue by using ParentClass.prototype.myMethod.call(this, arg1, arg2, ...);



So respectively



      a.prototype.initialize.call(this);



and



    a.prototype.foo.call(this);



I will leave the question open, in case a developer with more experience has more input on this issue.


View solution in original post

4 REPLIES 4

peterraeves
Mega Guru

Okay. So it seems that super is only available from ES6 and SNOW still uses ES5 (in Jakarta).



It seems I fixed the issue by using ParentClass.prototype.myMethod();



So respectively



      a.prototype.initialize();



and



    a.prototype.foo();



I will leave the question open, in case a developer with more experience has more input on this issue.


Thanks for the info peter, you should document this.


Okay, not there yet. It seems that the functions are called correctly, but onto a different object, because the variables are not correctly manipulated on the current object.



I tried the following:


var a = Class.create();  


a.prototype = {  


      initialize: function() {  


              gs.print('init a');


              this.var1 = 'Hello';


      },  


 


      foo: function() {  


              gs.print(this.var1 + ' World');  


      },


 


      type: 'a'  


};  


 


var b = Class.create();  


b.prototype = Object.extendsObject(a, {  


      initialize: function() {  


              a.prototype.initialize();


              gs.print('init b');  


      },  


 


      type: 'b'  


});  


 


var c = new b();  


c.foo();



And got:


*** Script: init a


*** Script: init b


*** Script: undefined World


Okay, second conslusion. It seems that super is only available from ES6 and SNOW still uses ES5 (in Jakarta).



It seems I fixed the issue by using ParentClass.prototype.myMethod.call(this, arg1, arg2, ...);



So respectively



      a.prototype.initialize.call(this);



and



    a.prototype.foo.call(this);



I will leave the question open, in case a developer with more experience has more input on this issue.