I deal with nodejs and i cannot figure out, why i cannot call class instance from another class function. Here is my code. Please help.
var StationDAO = require('./StationDAO.js');
var StationSearchCriteria = require('./StationSearchCriteria.js');
function GasStationService(lat, long, zoom) {
this.latitude = lat;
this.longitude = long;
this.zoom = zoom;
this.zoomToKilometers = 0.009;
/* ......... */
this.minLat = function () {
return this.latitude - this.zoomToKilometers;
};
this.minLong = function() {
return this.longitude - this.zoomToKilometers;
};
this.maxLat = function() {
return this.latitude + this.zoomToKilometers;
};
this.maxLong = function() {
return this.longitude + this.zoomToKilometers;
};
this.criteria = new StationSearchCriteria(this.minLat(), this.minLong(), this.maxLat(), this.maxLong());
this.conn = new StationDAO(criteria); // why this doesnt work
-
Is StationDAO module defined as a single function bound to module.exports?ilmirons– ilmirons2014年03月13日 10:06:42 +00:00Commented Mar 13, 2014 at 10:06
-
It is class accessing to Elastic search DB and it is module.exportsTomHonner– TomHonner2014年03月13日 11:13:47 +00:00Commented Mar 13, 2014 at 11:13
-
Is there any async code in StationDAO? (maybe something is not yet initialized when you are invoking it).ilmirons– ilmirons2014年03月13日 11:22:26 +00:00Commented Mar 13, 2014 at 11:22
-
yes, there is one function, but it isnt called.TomHonner– TomHonner2014年03月13日 11:25:39 +00:00Commented Mar 13, 2014 at 11:25
1 Answer 1
you are passing criteria which is not defined i assume you wanted to pass this.criteria isntead.
this.criteria = new StationSearchCriteria(this.minLat(), this.minLong(), this.maxLat(), this.maxLong());
this.conn = new StationDAO(this.criteria); //this should work
answered Mar 13, 2014 at 10:05
0x_Anakin
3,2695 gold badges52 silver badges91 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Balayesu Chilakalapudi
if ` criteria ` is not defined then how this.criteria is existing ?
0x_Anakin
this.criteria is not the same as criteria. I seriously dont understand your comment.
Balayesu Chilakalapudi
'this' pointer refers the current object's variable, in the problem statement 'criteria' is a variable which is the instance of StationSearchCriteria class and is already initialized.
barry-johnson
+1'd this to bring it to zero since I think someone downvoted it not understanding it. Or maybe I am not understanding. I don't know why someone thinks that
foo and this.foo would be the same thing.lang-js