0

This is how I create my module:

var $ = $ || false;

if (typeof jQuery !== 'undefined') {
    $ = jQuery;
}

var conversekitTemplates = {
    test1: function() {
        alert('hello everyone');
    },
    test2: function() {
        $('body').css('background-color', '#fff');
    },
    test3: 123,
    test4: 440,
    test5: function() {
        var a = apple;
    },
    test6: 'h1>' + a + '</h1>'; //how to use the value of variable a here?
};

This is how I'm calling it:

require.config({
    paths: {
        'jquery': '/media/jui/js/jquery.min',
        'templates': '/plugins/system/conversekit/templates'
    }
});

require(['jquery', 'templates'], function($) {   
    $('body').html('helo');
    // conversekitTemplates.test1();
    console.log(conversekitTemplates.test6); 
});

I did refer here: Accessing variables from other functions without using global variables

But I don't want to pass the value from one function to another in particular. I want to let any function that want to use a value from a function to be able to call it.

Community
  • 1
  • 1
112233
  • 2,406
  • 3
  • 38
  • 88
  • Let make sure I understand what you are asking. You are saying you have a module with a function in it and there is a variable with a value in that function (such as test5) and you want to be able to get the value of the variable in that function for anyone outside of that module. Yes? If so - no. The variable will always be LOCAL to that function unless you make it otherwise. You can try something like .. - but I do not believe it will work. Why not make it local the module? Those you can get easily. Or use a return in a function? Both should work. – Mark Manning Jul 21 '16 at 03:32
  • You can make variables accessible to modules if you write `global.foo = 'bar'` instead of `var foo = 'bar'`. –  Jul 21 '16 at 03:34
  • @MarkManning, I'm not accessing outside of the function. Since test6 is inside the namespace, i want that to access test5 value. So that when I call namespace.test6 it will show the test5's value together – 112233 Jul 21 '16 at 03:37
  • @SoftwareEngineer171 : That is true but the poster said without using global variables. :-/ – Mark Manning Jul 21 '16 at 03:41
  • @Keren : test6 <> test5. But both test5 & test6 are a part of the object called conversekitTemplates. This does make the functions a part of the same namespace but the variable inside of test5 is unique to test5. Or to put that another way - if you had two functions (say test5 & test7) and test5's variable is "apple" and test7's variable was "banana" then a return from test5 would give you "apple" and a return from test7 would give you "banana". The problem here really is that you just used "a" in test6. Use "test5()" instead. And use a return. Cleaner code that way. – Mark Manning Jul 21 '16 at 03:48
  • Doesn't make too much sense to me. If `a` is used in more than one place, why would you define it inside one function? – aIKid Jul 21 '16 at 03:51
  • @MarkManning, i did use return and test5()..but it logs out. function()... – 112233 Jul 21 '16 at 03:51
  • @Keren : Did you make test5:function(){return"apple";}? So it returns the word "apple"? Er...and I just noticed that "apple" does not have quotes around it either. So Javascript would think that "apple" actually is a memory location. Unknown what it would do with that. – Mark Manning Jul 21 '16 at 03:52
  • @Keren: That might be a syntax problem.. You sure you called the function with `()`? – aIKid Jul 21 '16 at 03:53
  • @Karen : basically, "a" is undefined. test5() is defined. Better? – Mark Manning Jul 21 '16 at 03:58
  • @Keren : something to try is to use a debugger on this. Javascript is an interpreted language so until you call test5() - a will never be defined. So you can't get the value of "a" until you call test5() - because it will not be defined. If you step through the debugger you will see that it just jumps from the var where you define conversekitTemplates to the end of it. It is not like a compiler. A compiler predefines everything but an interpreter only defines things as it needs them. Something else to keep in mind. :-) – Mark Manning Jul 21 '16 at 04:04

1 Answers1

0

I found the answer, in my post above I created object literal. That way I don't think can share variable between functions.

Now, I have to make module pattern like below:

var conversekitTemplates = (function(){

var id = 4;
  return {
    test1: function(){
      alert(id);
    },
    test2: id+3
  };
})();

and access it like this:

conversekitTemplates.test1();
alert(conversekitTemplates.test2);
112233
  • 2,406
  • 3
  • 38
  • 88