为什么变量未定义?

时间:2014-11-18 13:21:11

标签: javascript firefox firefox-addon

function foo(){
    var filename="xxx.txt";   
    var readtxt;
    var file = Cc["@mozilla.org/file/directory_service;1"].
            getService(Ci.nsIProperties).
            get("Home", Ci.nsIFile);
    file.append(filename);
    NetUtil.asyncFetch(file, function(inputStream, status) {
        if (!components.isSuccessCode(status)) {
            // Handle error!
            console.log("file read error.."+filename);
            return;
        }
        // The file data is contained within inputStream.
        // You can read it into a string with
        readtxt = NetUtil.readInputStreamToString(inputStream, inputStream.available());
        console.log("firstly print="+readtxt);
    });
    console.log("secondly print="+readtxt);
}

代码的目的是从文件中读取然后打印它。 在上面的代码中,我定义了一个var readtxt。然后在第console.log("firstly print="+readtxt);行中,成功打印readtxt。但是,在console.log("secondly print="+readtxt);行中,它显示为secondly print=undefined。我不知道为什么。

0 个答案:

没有答案