0

I'd like to call a variable, lets say var cow; which is placed in the function function farm() in the function function milk().

<script type="text/javascript">
var cow
function farm() {

cow = Math.Random;

}

function milk(){

// call/return value of cow

}
</script>

Check out this link: GLOBAL VARIABLES IN JAVASCRIPT by Snook .
Now if you see the last code carefully, and test it...it actually doesn't work, but I want something similar to work. You can check the code below...

var myValue;
function setValue()
{
    myValue = "test";
}

function getValue()
{
    alert(window.myValue); // yup, it's "test" (original) ---> (after testing) No its "undefined"
}
4
  • And does your test work or not? What do you get if you test for the existence & value of the variable? Anything? Not sure based on your message whether you have a problem. If I ever have to do anything like that, I declare a GLOBAL variable outside of any functions, in the wide open, like so: var myValue; and then I simply keep referring to myValue in other functions. It works (but it's not recommended). Commented Dec 8, 2011 at 18:38
  • @ShawnSpencer : I get undefined...tried it many times...but still its "undefined" Commented Dec 8, 2011 at 18:40
  • 1
    @tunetosuraj - Your original statement is correct, cow will be global. However, you're probably looking for Math.random() and not Math.Random (which is undeclared and thus undefined).. See my answer for more details. Commented Dec 8, 2011 at 18:49
  • @MikeChristensen Thanks buddy.. I'm sorry I missed your answer.. Commented Dec 11, 2011 at 13:17

3 Answers 3

2

Similar to your example you can declare cow in the global scope:

var cow;

function farm() {

  cow = Math.random();

}

function milk(){

  // call/return value of cow

}

here is a working fiddle http://jsfiddle.net/IrvinDominin/FTt5D/; pay attention random is a method, you must use parenthesis () and write it in lowercase.

Sign up to request clarification or add additional context in comments.

1 Comment

Dude you figured out what I was missing... :) well thanks a lot
2

What you have will actually work, as cow will be hoisted to the enclosing scope (which, in this case, is global). It might not work because the function is actually random(), not Random(), and it's a function not a variable - Try this:

function farm() {
   cow = Math.random();
}

function milk(){
   // call/return value of cow
   alert(cow);
}

farm(); //Init cow
milk(); //call milk

Comments

0

Declare cow in the global scope:

var cow;

function farm() {
  cow = Math.Random;
}

function milk() {
  // call/return value of cow
}

But I'm now sure how this differs from your second example. Could you clarify your question?

3 Comments

Nope. done that already...doesn't work...Am I missing something?
You probably have more code. Can you post it? This code works just fine: jsfiddle.net/kLgwU/4
In the second example, as it was stated on snook's site,...here window.somevariable is used...which is used to fetch a global variable...but that is not working for me

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.