0

function Method1(a, b, c) {
  this.a = a;
  this.b = b;
  this.c = c;
}

Method1.prototype = {
  add: function() {
    let x = this.a * Method2.r + this.b + this.c;
    console.log(x);
  }
}

function Method2(a, b, c, r) {
  this.r = r;
  this.obj = new Method1(a, b, c);
}

Method2.prototype = {
  print: function() {
    this.obj.add();
  }
}

let x = new Method2(1, 2, 3, 4);
x.print();

In the above sample code, I want to use the variable r of Method2 inside of Method1's add function. Can anyone please tell me where I am doing wrong and how to solve this? I need to access r inside add()

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
thenocturnalguy
  • 312
  • 3
  • 12
  • 3
    You can't just do `Method2.r`. How does it know which `r`? – Aplet123 Dec 18 '20 at 14:15
  • 3
    The way you've written your code, `r` is a property of *instances* of Method2, instances like `x`. Thus it doesn't make sense to access `r` inside the Method2 constructor. Instead, you should probably change `add()` so that it takes a Method2 instance as a parameter. – Pointy Dec 18 '20 at 14:15
  • It's `x` that has the `.r` property, not `Method2`. And no, you cannot access it from `add` at all. Use a parameter and pass it as an argument. – Bergi Dec 18 '20 at 14:15
  • Does this answer your question? https://stackoverflow.com/questions/34323263/instance-vs-reference-vs-object-in-javascript – Sergiu Paraschiv Dec 18 '20 at 14:16
  • @SergiuParaschiv No, why would it? – Bergi Dec 18 '20 at 14:16

3 Answers3

0

A workaround for you is to declare this.r for Method1. This way Method1 has can accept the r from Method2 and use it in the add function. See the attached snippet.

function Method1(a, b, c, r) {
    this.a = a;
    this.b = b;
    this.c = c;
    this.r = r;
}
Method1.prototype = {
    add: function() {
        let x = this.a * this.r + this.b + this.c;
        console.log(x);
    }
}

function Method2(a, b, c, r) {
    this.r = r;
    this.obj = new Method1(a, b, c, r);
}

Method2.prototype = {
    print: function() {
        this.obj.add();
    }
}

let x = new Method2(1, 2, 3, 4);

x.print();
Matt Croak
  • 2,788
  • 2
  • 17
  • 35
0

Consider making r in Method2 a static variable so that you can use it globally like this:

function Method1(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
}
Method1.prototype = {
    add: function() {
        let x = this.a * Method2.r + this.b + this.c;
        console.log(x);
    }
}

function Method2(a, b, c, r) {
    Method2.r = r; // This is the only change you need to make
    this.obj = new Method1(a, b, c);
}

Method2.prototype = {
    print: function() {
        this.obj.add();
    }
}

let x = new Method2(1, 2, 3, 4);
x.print();
dopesky
  • 190
  • 9
0

Did a little changes to your code:

function Method1(a, b, c) {
  this.a = a;
  this.b = b;
  this.c = c;
};
Method1.prototype.add = function(r){
  let x = this.a * r + this.b + this.c;
  console.log(x);
};
function Method2(a, b, c, r) {
  this.r = r;
  this.obj = new Method1(a, b, c);
}

Method2.prototype.print= function() {
  this.obj.add(this.r);
};

The problems were

  1. You were defining prototype functions wrong so instead of

    Method2.prototype = { print: function() { this.obj.add(); }}

    you should use:

    Method2.prototype.print = function() {this.obj.add();}

  2. Method2 is independent of Method1, so in order to use the "r" from Method2 in Method1 it needs to be passed from Method2 to Method1.

Alex Dragnea
  • 174
  • 1
  • 6