访问变量外部函数

时间:2012-09-13 07:04:48

标签: javascript global-variables scope

我有一个带有函数的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未由用户定义;它是从Feed加载的,因此我无法在convertTime()函数之前定义它。

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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