如果我声明一个变量并从一个方法赋值并在方法外打印。但它显示未定义。我的代码是,
var value;
var getValue = function getValue(){
value = 5;
};
console.log(value);
输出,
undefined
我也在尝试全局变量
var getValue = function getValue(){
global.value = 5;
};
console.log(value);
但显示一些错误,
console.log(value);
^
ReferenceError: value is not defined
at Object.<anonymous> (~/MyApp/test.js:8:13)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3