变量未定义 - javascript
Variable not defined - javascript
我正在编写一个 AI。它不工作。浏览器显示:Uncaught ReferenceError: do is not defined.
var what = ["jokes", "cats", "news", "weather", "sport"];
function start() {
var do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
var what = ["jokes", "cats", "news", "weather", "sport"];
var do;
function start() {
do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
do 在这里是变量而不是函数。
var do = what[Math.floor((Math.random() * what.length) + 1)];
要创建一个 do 函数,您可以这样做。
var what = ["jokes", "cats", "news", "weather", "sport"];
var do;
function start() {
do = function(){ return what[Math.floor((Math.random() * what.length) + 1)]};
}
start();
Document.write(do());
Do 仅存在于您的函数中。阅读有关功能范围的信息:)试试这个:
var what = ["jokes", "cats", "news", "weather", "sport"];
var do = undefined;
function start() {
do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
你做的变量超出范围
var what = ["jokes", "cats", "news", "weather", "sport"];
function start() {
var do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
您需要将密码更改为
var what = ["jokes", "cats", "news", "weather", "sport"];
function start(callback) {
var do = what[Math.floor((Math.random() * what.length) + 1)];
callback(do);
}
start(function(val) {document.write(val)});
我正在编写一个 AI。它不工作。浏览器显示:Uncaught ReferenceError: do is not defined.
var what = ["jokes", "cats", "news", "weather", "sport"];
function start() {
var do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
var what = ["jokes", "cats", "news", "weather", "sport"];
var do;
function start() {
do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
do 在这里是变量而不是函数。
var do = what[Math.floor((Math.random() * what.length) + 1)];
要创建一个 do 函数,您可以这样做。
var what = ["jokes", "cats", "news", "weather", "sport"];
var do;
function start() {
do = function(){ return what[Math.floor((Math.random() * what.length) + 1)]};
}
start();
Document.write(do());
Do 仅存在于您的函数中。阅读有关功能范围的信息:)试试这个:
var what = ["jokes", "cats", "news", "weather", "sport"];
var do = undefined;
function start() {
do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
你做的变量超出范围
var what = ["jokes", "cats", "news", "weather", "sport"];
function start() {
var do = what[Math.floor((Math.random() * what.length) + 1)];
}
start();
Document.write(do);
您需要将密码更改为
var what = ["jokes", "cats", "news", "weather", "sport"];
function start(callback) {
var do = what[Math.floor((Math.random() * what.length) + 1)];
callback(do);
}
start(function(val) {document.write(val)});