Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

为什么return data 之后,显示data is not defined?

function getData() {
    var data = new Array();
  for (var i=0; i<list.length; i++) {
    var city_text = list[i].firstChild.nodeValue;
    var city = city_text.substring(0,2);    //截取字符串,从0到2,但不包括2.
    var num = list[i].lastChild.innerHTML;
    data[i] = [city,num];
    }
  alert(data.length);    //7
  return data;
}
alert(data.length);    //data is not defined

已经return data了,外部的data为何无法调用呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你所说的data是定义在getData方法中的一个局部变量,在外面是不能使用的,你可以使用一个变量接受返回值
var myData =getData()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...