user1765862 user1765862 - 6 months ago 13
Javascript Question

js namespaces and variable scope

If I have custom javascript namespace like with following structure

myns.controllers.home = {
init = function(){
},
doSomething: function(){
var cars = 100; // simple example
var url = 'someurl';
myns.ajaxHelper.execute(url, { id: id }, myns.controllers.home.jobUtils.callback);
}
}
myns.controllers.home.jobUtils.callback = function (cars) {
alert(cars.lenght); // cars is undefined?
}


why cars variable is undefined here and how would you solve/refactor this?

Answer

I'd try declaring variable as a ns property:

myns.controllers.home = {
  cars: 100,
  init: ...
Comments