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


let's have object foo

var foo = function() {   var array = [];   var method = function() {return true;}; }; 

and foo.array contain object:

var bar = function() {   var method = function() {/*perform foo.method() here */ }; }; 

so var foo.array = [new bar()];

how can access foo object bar instance array element.

this incorrect:

var foo.array = [new bar()]; 

you need this:

var foo = new foo(); 

and can this:

foo.array = [new bar(foo)]; 

but need modify bar class can store reference foo, let's this:

var bar = function(foo) {   // here save object reference   // accesible function scope   var _foo = foo;    var method = function() {      _foo.method(); // example calling   }; }; 

and can access foo variable inside bar class , whatever need it. foo.method() , foo.array not visible, need make them public:

var foo = function() {   this.array = [];   this.method = function() {return true;}; }; 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -