- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2023 08:07 AM
Hello, I have created 2 script includes. One is named Calculator which is the parent, and the other is Calculator2 child which extends the parent. Is it possible to use variables from the parent script include?
More specifically I want to use the variable num value from parent(Calculator) to the method mul for the child script include(Calculator2).
Calculator2 - child
Calculator - Parent
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2023 09:38 AM
Running this:
var Calculator = Class.create();
Calculator.prototype = {
initialize: function () {
this.num = 10;
},
add: function (n1, n2) {
return n1 + n2;
},
sub: function (n1, n2) {
return n1 - n2;
},
mul: function (n1) {
return n1 * this.num;
},
type: 'Calculator'
};
var Calculator2 = Class.create();
Calculator2.prototype = Object.extendsObject(Calculator, {
initialize: function () {
Calculator.prototype.initialize.apply(this, arguments);
},
add: function (n1, n2) {
return n1 + n2;
},
sub: function (n1, n2) {
return n1 - n2;
},
mul: function (n1) {
return n1 * this.num;
},
type: 'Calculator2'
});
var c2 = new Calculator2();
gs.debug(c2.mul(2));
in Scripts - Background prints
*** Script: [DEBUG] 20
So the key is modifying Calculator2's initialize and adding:
Calculator.prototype.initialize.apply(this, arguments);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2023 09:04 AM
Not as Calculator is defined now. When you create an instance of Calculator2 the properties will be combined, but the initialize method of Calculator will not be auto-magically executed. You need to explicitly call it in the initialize method of Calcualtor2.
If you will be kind enough to post code not picture, I will be kind enough to modify it and show how.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2023 09:34 AM
var Calculator2 = Class.create();
Calculator2.prototype = Object.extendsObject(Calculator,{
initialize: function() {
},
add:function(n1,n2){
return n1 + n2;
},
sub:function(n1,n2){
return n1 - n2;
},
mul:function(n1){
return n1*this.num;
},
type: 'Calculator2'
});
var Calculator = Class.create();
Calculator.prototype = {
initialize: function() {
this.num = 10;
},
add:function(n1,n2){
return n1 + n2;
},
sub:function(n1,n2){
return n1 - n2;
},
mul:function(n1){
return n1*this.num;
},
type: 'Calculator'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2023 09:38 AM
Running this:
var Calculator = Class.create();
Calculator.prototype = {
initialize: function () {
this.num = 10;
},
add: function (n1, n2) {
return n1 + n2;
},
sub: function (n1, n2) {
return n1 - n2;
},
mul: function (n1) {
return n1 * this.num;
},
type: 'Calculator'
};
var Calculator2 = Class.create();
Calculator2.prototype = Object.extendsObject(Calculator, {
initialize: function () {
Calculator.prototype.initialize.apply(this, arguments);
},
add: function (n1, n2) {
return n1 + n2;
},
sub: function (n1, n2) {
return n1 - n2;
},
mul: function (n1) {
return n1 * this.num;
},
type: 'Calculator2'
});
var c2 = new Calculator2();
gs.debug(c2.mul(2));
in Scripts - Background prints
*** Script: [DEBUG] 20
So the key is modifying Calculator2's initialize and adding:
Calculator.prototype.initialize.apply(this, arguments);