Let's say I have object Foo
var Foo = function() {
var array = [];
var method = function() {return true;};
};
And Foo.array will contain another object:
var Bar = function() {
var method = function() {/*Perform Foo.method() here */ };
};
So for var foo.array = [new Bar()];
How can I access foo object from the bar instance array element.
this is incorrect:
var foo.array = [new Bar()];
you need to do this:
var foo = new Foo();
and then you can do this:
foo.array = [new Bar(foo)];
but you need to modify your Bar class
so you can store a reference to foo
, let's say something like this:
var Bar = function(foo) {
// here you will save an object reference
// that will be accesible for all function scope
var _foo = foo;
var method = function() {
_foo.method(); // an example calling
};
};
and then now you can access to foo
variable inside your Bar
class and do whatever you need with it. But your Foo.method()
and Foo.array
are not visible, you need to make them public:
var Foo = function() {
this.array = [];
this.method = function() {return true;};
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments