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

5
  • 3
    You can't just do Method2.r. How does it know which r? Commented Dec 18, 2020 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. Commented Dec 18, 2020 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. Commented Dec 18, 2020 at 14:15
  • Does this answer your question? stackoverflow.com/questions/34323263/… Commented Dec 18, 2020 at 14:16
  • @SergiuParaschiv No, why would it? Commented Dec 18, 2020 at 14:16

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.