0

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

3
  • You need to execute mufunction2 to return the functions. Commented Oct 18, 2013 at 9:51
  • why would you want to return a function and not just give back the value? Commented Oct 18, 2013 at 9:53
  • @JamieHutber: this seems to be based on some sort of example code - I've seen this style of thing as examples of closures with lessons about JavaScript. Commented Oct 18, 2013 at 9:56

2 Answers 2

3

You could do:

myfunction2: function(message1,message2){

    var value1 = message1;
    var value2 = message2;

    namespace.myfunction2.getMess: function(){ return value1;}
    namespace.myfunction2.getLab: function() { return value2;}
}

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.

var namespace = (function() {

    // Private members
    var value1, value2;

    return {

      // Privileged methd to read private member values
      fn1: function() {
        return namespace.fn2.getMess1();
      },

      // Privileged methods to set and get private member values
      fn2: {
        setMess: function(message1, message2) {
          value1 = message1;
          value2 = message2;
        },

        getMess1: function() {
          return value1;
        },

        getMess2: function() {
          return value2;
        }
      }
    }
}());

namespace.fn2.setMess("hello","bye");

alert(namespace.fn1()); // hello
Sign up to request clarification or add additional context in comments.

3 Comments

Do we really need to have that outer return (just after declaring private members), reason being, my code has a lot of other functions in the namespace along with fn1 and fn2? Also, looks like we are making use of global variables value1, value2 (correct me if wrong), so would that be really necessary. I initially thought of using global variables, and through that it can be done, but I didnt want to.
The first return is required, it provides the object that is assigned to namespace. Also, value1 and value2 aren't global, they're "private" variables held in a closure, they can only be accessed or modified by "privileged" members such as the set/getMess functions.
Thanks Rob, I understand it now.
0

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.

var namespace ={
    myfunction1: function(){
        var jamie = namespace.myfunction2("hello","bye");   // save returned value so we can use them later.
        console.info(jamie.getMess); //as we already executed the value just refer to them
    },

    myfunction2: function(message1,message2){
        var value1 = message1;
        var value2 = message2;
        return{
          getMess: function(){ return value1;}(), //return self executing functions to return values without the need to run them again.
          getLab: function(){ return value2;}()
        }
    }
}
namespace.myfunction1();

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.

1 Comment

Well, the call to namespace.function2 with the parameters has to be from outside only, since I am calling it from a different file (cshtml) and my rest of the code is in a separate js file. I will try to summarize what I want - I want to set 2 values (which the call to namespace.myfunction2 is doing) in a myfunction2, then i want those value to be accessed from another function i.e. myfunction1. I dont mind if the whole code is changed, but I would like to achieve that. Hope that clarifies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.