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"
}
Similar to your example you can declare cow in the global scope:
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.