简体   繁体   English

无法访问在 ZDE9B9ED78D7E2E1DCEEFFEE780E2F91 中另一个 object 的 function 内部创建的 object 的属性

[英]Cannot access property of an object created inside function of another object in javascript

 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.在上面的示例代码中,我想在 Method1 的 add function 中使用 Method2 的变量 r。 Can anyone please tell me where I am doing wrong and how to solve this?谁能告诉我我做错了什么以及如何解决这个问题? I need to access r inside add()我需要在 add() 中访问 r

A workaround for you is to declare this.r for Method1 .一种解决方法是为Method1 this.r This way Method1 has can accept the r from Method2 and use it in the add function.这样, Method1可以接受rMethod2并在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();

Consider making r in Method2 a static variable so that you can use it globally like this:考虑在r中将Method2设为 static 变量,以便您可以像这样全局使用它:

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

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(); Method2.prototype = { print: function() { this.obj.add(); }} }}

    you should use:你应该使用:

    Method2.prototype.print = function() {this.obj.add();} 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. Method2 独立于 Method1,因此为了在 Method1 中使用 Method2 中的“r”,需要将它从 Method2 传递给 Method1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM