简体   繁体   English

如何访问另一个原型对象的变量

[英]How to Access to another Prototype Object's variable

A class that has an array and getter/setter. 一个具有数组和getter / setter的类。

var MyObject = (function MyObject() {
    this.arr = [];

    MyObject.prototype.addArr =  function(i) {
        this.arr.push(i);
    };
    MyObject.prototype.getArr =  function() {
        return this.arr;
    };
});

And another object try to access to array of MyObject . 并且另一个对象尝试访问MyObject数组。

var AnotherObject = (function AnotherObject() {
    AnotherObject.prototype.addToMyObject =  function(i, MyObject) {
        MyObject.addArr(i); // here's the problem!
    };
    AnotherObject.prototype.getArr =  function(MyObject) {
        return MyObject.getArr();
    };
});

Now test. 现在测试。

var a = new MyObject();    
a.addArr(1);
a.addArr(2);
a.addArr(3);

// works good.
console.log(a.getArr().pop()); // 3
console.log(a.getArr()); // [1,2]

var b = new AnotherObject();
b.addToMyObject(4);
b.addToMyObject(5);
b.addToMyObject(6);

console.log(b.getArr().shift()); // error!
console.log(b.getArr());

It says AnotherObject.addToMyObject() is wrong. 它说AnotherObject.addToMyObject()是错误的。 'addArr' is not defined or null inside of addToMyObject() . addToMyObject()未定义'addArr'或为null。

Correct me how to access another Object's variable 纠正我如何访问另一个对象的变量

http://jsfiddle.net/amhk9o2a/2/ http://jsfiddle.net/amhk9o2a/2/

The methods of AnotherObject expect a MyObject to be passed as an argument. AnotherObject的方法期望将MyObject作为参数传递。 So it should be: 因此应该是:

b.addToMyObject(4, a);
b.addToMyObject(5, a);
b.addToMyObject(6, a);
console.log(b.getArr(a).shift());
console.log(b.getArr(a));

Since you weren't passing this argument, the MyObject parameter variable was being set to undefined , so you were calling undefined.addArr(i) and undefined.getArr() . 由于未传递此参数,因此将MyObject参数变量设置为undefined ,因此您在调用undefined.addArr(i)undefined.getArr()

Here is a better example of a Constructor: 这是构造函数的一个更好的示例:

function MyObject(){
  this.arr = [];
  this.addArr = function(i){
    this.arr.push(i);
  }
  this.getArr = function(){
    return this.arr;
  }
}
function AnotherObject(){
}
AnotherObject.prototype = MyObject.prototype;
// now AnotherObject has MyObject prototype

I've made several changes through the code. 我已经通过代码进行了一些更改。 To make it easier to follow I've labelled sections A, B and C. 为了更容易理解,我将A,B和C部分标记为。

  • A , fixed the constructor definition for MyObject so the prototype is not destroyed and re-created every time we create an instance of it A 修复了MyObject的构造函数定义,因此在我们每次创建原型时都不会销毁并重新创建原型
  • B , fixed the constructor definition for AnotherObject so the prototype is not destroyed and re-created every time we create an instance of it B 修复了AnotherObject的构造函数定义,因此在我们每次创建原型实例时都不会破坏并重新创建原型
  • B , taught it a new method where we pick a target object to work on B 教给它一种新方法,我们选择一个目标对象进行处理
  • C , constructed instances, with the AnotherObject instance I used the new method to target the MyObject instance C 构造AnotherObject实例,以及AnotherObject实例,我使用了新方法来定位MyObject实例
// --- A ---
function MyObject() {
    this.arr = [];
}
MyObject.prototype.addArr = function (i) {
    this.arr.push(i);
};
MyObject.prototype.getArr = function () {
    return this.arr;
};

// --- B ---
function AnotherObject() {
}
AnotherObject.prototype.setTarget = function (myObject) { // extra bit
    this.target = myObject;
};
AnotherObject.prototype.addToMyObject = function (i) {
    this.target.addArr(i); // this now points to a new place
};
AnotherObject.prototype.getArr = function(MyObject) {
    return this.target.getArr(); // this now points to a new place
};

// --- C ---
var a = new MyObject(),
    b = new AnotherObject();
b.setTarget(a); // Enemy sighted, assigning it as the target!!!!
b.addToMyObject('foo');
b.getArr(); // ["foo"]
// notice this also means
a.getArr(); // ["foo"]

The previous style of writing the constructor you were attempted looked as if it was intended to be the following, using an IIFE 使用IIFE之前尝试编写构造函数的样式看起来像是下面的样子

var MyObject = (function () {
    function MyObject() {
        this.arr = [];
    }
    MyObject.prototype.addArr = function (i) {
        this.arr.push(i);
    };
    MyObject.prototype.getArr = function () {
        return this.arr;
    };
    return MyObject;
}());

This is a perfectly valid way to do it to keep the namespace clean, but it more complicated to read. 这是保持名称空间整洁的一种完全有效的方法,但是读取起来更加复杂。 If you're new to constructors or IIFEs , I'd suggest learning them independently from each other first. 如果您是构造函数或IIFE的新手 ,建议您首先独立地学习它们。

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

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