简体   繁体   中英

How to Access to another Prototype Object's variable

A class that has an array and 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 .

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. 'addArr' is not defined or null inside of addToMyObject() .

Correct me how to access another Object's variable

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

The methods of AnotherObject expect a MyObject to be passed as an argument. 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() .

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 , fixed the constructor definition for MyObject so the prototype is not destroyed and re-created every time we create an instance of it
  • 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 , taught it a new method where we pick a target object to work on
  • C , constructed instances, with the AnotherObject instance I used the new method to target the MyObject instance
// --- 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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