Javascript 访问全局变量时出现问题

Javascript 访问全局变量时出现问题,javascript,character-encoding,error-handling,Javascript,Character Encoding,Error Handling,这是我的代码: function Todo(id, task, who, dueDate) { this.id = id; this.task = task; this.who = who; this.dueDate = dueDate; this.done = false; } var todos = new Array(); window.onload = init; function init() { var submitButton

这是我的代码:

function Todo(id, task, who, dueDate) {
    this.id = id;
    this.task = task;
    this.who = who;
    this.dueDate = dueDate;
    this.done = false;
}

var todos = new Array();

window.onload = init;

function init() {
    var submitButton = document.getElementById("submit");
    submitButton.onclick = getFormData;
    var searchButton = document.getElementById("button");
    searchButton.onclick = search;       
}


//function to add todo items to the todos array

function search() {
  for (var i = 0; i < todos.legnth; i++) {
     var todoObj = todos[i];
     console.log(todoObj[0]);
    }
} 
功能待办事项(id、任务、人员、截止日期){
this.id=id;
this.task=任务;
这个。谁=谁;
this.dueDate=dueDate;
this.done=false;
}
var todos=新数组();
window.onload=init;
函数init(){
var submitButton=document.getElementById(“提交”);
submitButton.onclick=getFormData;
var searchButton=document.getElementById(“按钮”);
searchButton.onclick=搜索;
}
//函数将todo项添加到todo数组
函数搜索(){
对于(var i=0;i

这不是我的全部代码,但最后一个函数是我遇到问题的地方。我似乎无法访问全局todos数组。我尝试将其作为参数传递给搜索函数。我甚至试过做I 问题是你的打字错误

替换

todos.legnth


尝试将legnth更改为length。
todos.length