From an Object's Array's Element, how can I access the Object's properties/methods. Js

user2800382

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.

Santiago Hernández

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I access array's item when there is an object in it?

How can I access an object's components from a different scene?

how can i get an object's value from an array?

How can I access an object within another object's method?

How can I push to an array that’s inside an object's element in JavaScript?

How to access object's members from an array of object tuples

Can't access a property of an object if it's in an array

How can I use jQuery to access an element that's stored in an array?

I can't access an object's properties

How can i access the local player's Network object?

How can I push to an array thats inside an object's element in Javascript

How can I return the array object from Array's prototype function?

How to access object's array value in interpolation?

How can I access an object's method from another class giving the class not to much visibility?

How can I put the values of an object's property into an array?

I can't access to my object's array's values for some reason

How can I get the object from a button's value?

How can i update array's element?

How do I access this hash's object's attributes in Ruby?

How to access an array´s element from hashmap´s values? Java

Mongoose: How can I retrieve an object element stored insider another main object by a field's value?

How can I access a JavaScript object which has spaces in the object's key?

How can I access an object of type A that's within an object of type B using pipes and mapping?

How to access parent object's parent from an object efficiently?

How do I access an object's keys' values? Looking to populate a table from object data

How to print object's element?

How to access object's fields

How can I use a string's value as an object's name?

How can I set an Element's Id in js by it's name?