访问函数外部的变量



我有一个javascript代码,其中包含以下函数:

function convertTime()
function loadZive()

假设我有以下代码:

function convertTime(){
    alert(exampleVariable);
    };
function loadZive(){
    var exampleVariable = 3;
    convertTime();
    };

我的convertTime()函数似乎无法访问exampleVariable。有什么变通办法吗?

这是完整的代码:

function convertTime(publishedDate){
    alert(publishedDate);
    var date = publishedDate;
    var publishedDay = date.toLocaleDateString();
    var publishedHour = date.getHours();            //nemozno pouzit toLocaleTimeString (cielovy format ma byt bez sekund)
    if (publishedHour < 10){
        publishedHour = "0" + publishedHour;
    };
    var publishedMinute = date.getMinutes();        //nacitanie hodin a minut
    if (publishedMinute < 10){
        publishedMinute = "0" + publishedMinute;
    };
    var publishedTime = publishedHour +":"+ publishedMinute;    //ich nasledne spojenie do casoveho formatu HH:MM
    var publishedDateConverted = publishedDay +", "+ publishedTime;
};
function loadZive(publishedDateConverted){
google.load("feeds", "1");  
    function initialize() {
        var feed = new google.feeds.Feed("http://www.zive.sk/rss/sc-47/default.aspx");
        feed.setNumEntries(window.localStorage.getItem("entriesNumber"));
        feed.load(function(result) {
            if (!result.error) {
                var feedlist = document.getElementById("feedZive");
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var li = document.createElement("li");
                    var entry = result.feed.entries[i];
                    var A = document.createElement("A");
                    var descriptionSettings = window.localStorage.getItem("descriptionSettings");
                    if (descriptionSettings=="true"){
                        var h3 = document.createElement("h3");
                        var p = document.createElement("p");
                        var pDate = document.createElement("p");
                        pDate.setAttribute("class","ui-li-aside");
                        var publishedDate = new Date(entry.publishedDate);
                        convertTime();
                        pDate.appendChild(document.createTextNode(publishedDateConverted));
                        h3.appendChild(document.createTextNode(entry.title));
                        p.appendChild(document.createTextNode(entry.content));
                        A.setAttribute("href",entry.link);
                        A.appendChild(h3);
                        A.appendChild(p);
                        A.appendChild(pDate);
                        }
                    else{
                        A.setAttribute("href",entry.link);
                        A.appendChild(document.createTextNode(entry.title));
                    };
                    li.appendChild(A);
                    feedlist.appendChild(li);
                }
                $("#feedZive").listview("refresh");
            }
        });
    }
    google.setOnLoadCallback(initialize);   
};

警报结果为"未定义"。PS:exampleVariable不是由用户定义的;它是从提要加载的,所以我不能在convertTime()函数之前定义它。

正如Danny所说,或者您也可以通过以下方式传递变量:

function convertTime(exampleVariable){
    alert(exampleVariable);
};
function loadZive(){
    var exampleVariable = 3;
    convertTime(exampleVariable);
};

loadZive()中删除var,这使变量仅为loadZive()的本地变量。

或者,将exampleVariable作为参数传递给convertTime()

相关内容

  • 没有找到相关文章

最新更新