0

How to use the global variable topic outside the function which is assigned to global variable tell.

<html>
<head>
<script>
alert(topic);
tell = function(){
topic = "something";
};
</script>
</head>
<body>
</body>
</html>

Eventually I want to use the value of topic_splitted anywhere outside the function.

client.onMessageArrived = function (message) {
        topic_splitted = message.destinationName.split("/");
        console.log("Topic Splitted: " + topic_splitted);
        console.log("Message Topic: " + message.destinationName);
        console.log("Message Arrived: " + message.payloadString);
        if (message.destinationName == "in_progress"){
            if (message.payloadString == "false") {
                stepCode();
            }
        }

        var new_data = JSON.parse(message.payloadString);

        $.extend(true, data, data, new_data);
    };
  • topic has to be defined before alert and function via `var topic;` before it can be used – A. L Jan 10 '17 at 22:25
  • You need to call `tell()` somewhere to assign the variable – Bergi Jan 10 '17 at 22:42
  • Define `topic_splitted` outside the function, within scope of functions using it. – traktor Jan 11 '17 at 00:39
  • @Traktor53 I didn't get what you mean sorry. can you please explain that would be a great help. Thank you so much – Shawnik Raghav Jan 11 '17 at 00:53
  • Regarding your edit with the `onMessageArrived` listener, you should not use global variables at all. [Use callbacks instead](http://stackoverflow.com/q/14220321/1048572) to pass the result around. – Bergi Jan 11 '17 at 02:21

2 Answers2

0

It looks like you are not actually running the function "tell". Try this:

<html>
  <head>
    <script>
      var topic;
      var tell = function(){
        topic = "something";
      };
      tell()
      alert(topic);
    </script>
   </head>
  <body>
  </body>
</html>
Elliot Plant
  • 668
  • 6
  • 12
  • I want to use the topic variable before function like this ` ` – Shawnik Raghav Jan 10 '17 at 23:25
  • then order your stuff properly – A. L Jan 10 '17 at 23:43
  • @ShawnikRaghav you can't call a function stored in a variable before the variable has been set: variable declarations are hoisted but setting their values occurs where it is written. Prior to being set the hoisted variable has a value of `undefined` Try using a named function: `function tell() { //body}` – traktor Jan 10 '17 at 23:48
  • @Traktor53 This is what the original code is and I want to use the value on topic_splitted `client.onMessageArrived = function (message) { topic_splitted = message.destinationName.split("/"); console.log("Topic Splitted: " + topic_splitted); console.log("Message Topic: " + message.destinationName); console.log("Message Arrived: " + message.payloadString); if (message.destinationName == "in_progress"){ if (message.payloadString == "false") { stepCode(); } } var new_data = JSON.parse(message.payloadString); $.extend(true, data, data, new_data); };` – Shawnik Raghav Jan 11 '17 at 00:20
  • that length of code is not really suited to a comment - plus I don't immediately see how it relates to the question. Please edit the question to better explain what the problem is and what went wrong. – traktor Jan 11 '17 at 00:23
0

It is not a good idea to use undeclared variables, such as topic in the tell function of the first post example.

If you assign a value to an undeclared variable, JavaScript creates a global variable for you unless the code is running in strict mode. In strict mode it takes the safer approach of throwing an error that the variable has not been defined.

Where to declare common variables?

If the variable needs to be accessed by code executed at global level when the page loads, it needs to be declared at global level. This is not regarded as desirable and should be avoided if possible.

The alternative is to declare the variable inside a function which might be immediately invoked (an IIFE) or is executed in response to window load or document ready events.. Variables declared within such functions can be accessed by code within the outer function and all functions nested within it.

So if topic_splitted is defined outside all the functions which use it, preferable within a common outer function, there should be no problem.

traktor
  • 17,588
  • 4
  • 32
  • 53