How to access variable of parent script include

Community Alums
Not applicable

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

 

Calculator2.png

 

Calculator - Parent

Calculator.png

1 ACCEPTED SOLUTION

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);

View solution in original post

3 REPLIES 3

-O-
Kilo Patron
Kilo Patron

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.

Community Alums
Not applicable
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'
};

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);