我似乎无法从函数返回对象,函数内部的 console.log 可以很好地打印出属性值,但是一旦在函数之外我得到“未捕获的 ReferenceError:firstOn 未定义”
任何帮助将不胜感激,谢谢!
myElement = document.getElementById("testButton");
function Server(name,tables,startTime) {
this.name = name;
this.tables = tables;
this.startTime = startTime;
}
document.forms.server1.button.onclick = function() {
var name = document.forms.server1.servername.value;
var tables = document.forms.server1.servertables.value;
var startTime = document.forms.server1.servertime.value;
var firstOn = new Server(name,tables,startTime);
document.forms.server1.button.innerHTML = "Saved!";
console.log(firstOn.name);
console.log(firstOn.tables);
console.log(firstOn.startTime);
return firstOn;
};
myElement.onclick = function() {
console.log(firstOn.name);
console.log(firstOn.tables);
console.log(firstOn.startTime);
};