I am trying to access a variable which exists in another function, but I am not able to, it gives me undefined for the function through which (getMess() as below) I am doing that. As per the code below, I want the "value1" accessed through myfunction1, as shown below. Code:
var namespace ={
myfunction1: function(){
namespace.myfunction2.getMess(); // I need to access value1 here in this function
},
myfunction2: function(message1,message2){
var value1 = message1;
var value2 = message2;
return{
getMess: function(){ return value1;}
getLab: function() { return value2;}
}
}
}
namespace.myfunction2("hello","bye"); // this basically just sets the 2 values on page load
I just posted another question with the original problem : Read resource file entry in javascript - MVC application
You could do:
but that's pretty awful (assigning properties to a function object). Better to refactor the whole thing using the module pattern to emulate private and privileged members.
e.g.
This seems very strange to me, but first off you were retuning an object and missing a
,
inbetween the 2 functions you were trying to return.While I'm still not sure what you're trying to achieve this is how I would pass values between 2 functions whilst not declaring variables to
namespace
and just assigning values globally that way.