我正在编写一个AI。它不工作。浏览器说:Uncaught ReferenceError:do没有定义。变量未定义 - javascript
var what = ["jokes", "cats", "news", "weather", "sport"];
function start() {
var do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
阅读有关JavaScript函数范围(基本上变量中定义的变量只在该函数内部可见) – mic4ael
像mic4ael所说,这是Javascript中“范围”的问题。 'do'是在一个函数中定义的,因此在外面不可用。如果你在函数之外初始化'做',你将可以访问它。 –
http://stackoverflow.com/documentation/javascript/480/scope#t=201608182147440316607 –