2016-05-14 25 views
-2

我是Javascript的初學者,我被賦予了一項任務。這基本上是一個天氣應用程序.. 我創建了一個頁面,將輸出基於谷歌地理定位API的地方的經度,緯度和綽號。現在我想打電話給forecast.io,讓我知道結果。 我應該做的是將經緯度和暱稱存儲在LocalStorage中......點擊「保存位置」按鈕並將所有位置保存到列表中,以便點擊並獲取天氣信息。 但後來我得到了一個骨架代碼,我不知道它的功能。 我在後面寫的this.AddLocation和savelocation()函數之間有什麼區別。 我在這裏寫的唯一功能是將位置保存到本地存儲的savelocation()函數。其他函數是需要填充的骨架代碼。緩存! saveLocations()和addLocation()函數有什麼區別?

任何關於這個方法應該做什麼的解釋都會有很大的幫助!

的代碼如下:

// Returns a date in the format "YYYY-MM-DD". 
Date.prototype.simpleDateString = function() { 
    function pad(value) 
    { 
     return ("0" + value).slice(-2); 
    } 

    var dateString = this.getFullYear() + "-" + 
      pad(this.getMonth() + 1, 2) + '-' + 
      pad(this.getDate(), 2); 

    return dateString; 
} 

// Date format required by forecast.io API. 
// We always represent a date with a time of midday, 
// so our choice of day isn't susceptible to time zone errors. 
Date.prototype.forecastDateString = function() { 
    return this.simpleDateString() + "T12:00:00"; 
} 


// Code for LocationWeatherCache class and other shared code. 

// Prefix to use for Local Storage. You may change this. 
var APP_PREFIX = "weatherApp"; 

function LocationWeatherCache() 
{ 
    // Private attributes: 

    var locations = []; 
    var callbacks = {}; 

    // Public methods: 

    // Returns the number of locations stored in the cache. 
    // 
    this.length = function() { 
    }; 

    // Returns the location object for a given index. 
    // Indexes begin at zero. 
    // 
    this.locationAtIndex = function(index) { 
    }; 

    // Given a latitude, longitude and nickname, this method saves a 
    // new location into the cache. It will have an empty 'forecasts' 
    // property. Returns the index of the added location. 
    // 
    this.addLocation = function(latitude, longitude, nickname) 
    { 
    } 

    // Removes the saved location at the given index. 
    // 
    this.removeLocationAtIndex = function(index) 
    { 
    } 

    // This method is used by JSON.stringify() to serialise this class. 
    // Note that the callbacks attribute is only meaningful while there 
    // are active web service requests and so doesn't need to be saved. 
    // 
    this.toJSON = function() { 
    }; 

    // Given a public-data-only version of the class (such as from 
    // local storage), this method will initialise the current 
    // instance to match that version. 
    // 
    this.initialiseFromPDO = function(locationWeatherCachePDO) { 
    }; 

    // Request weather for the location at the given index for the 
    // specified date. 'date' should be JavaScript Date instance. 
    // 
    // This method doesn't return anything, but rather calls the 
    // callback function when the weather object is available. This 
    // might be immediately or after some indeterminate amount of time. 
    // The callback function should have two parameters. The first 
    // will be the index of the location and the second will be the 
    // weather object for that location. 
    // 
    this.getWeatherAtIndexForDate = function(index, date, callback) { 
    }; 

    // This is a callback function passed to forecast.io API calls. 
    // This will be called via JSONP when the API call is loaded. 
    // 
    // This should invoke the recorded callback function for that 
    // weather request. 
    // 
    this.weatherResponse = function(response) { 
    }; 

    // Private methods: 

    // Given a latitude and longitude, this method looks through all 
    // the stored locations and returns the index of the location with 
    // matching latitude and longitude if one exists, otherwise it 
    // returns -1. 
    // 
    function indexForLocation(latitude, longitude) 
    { 
    } 
} 

// Restore the singleton locationWeatherCache from Local Storage. 
// 
function loadLocations() 
{ 
} 

// Save the singleton locationWeatherCache to Local Storage. 
// 
function saveLocations(nickname,latitude,longtitude){ 
var locations = JSON.parse(localStorage.getItem('APP_PREFIX')) || []; 
    locations.push({nickname: nickname, latitude: latitude, longtitude:longtitude}); 
    localStorage.setItem('APP_PREFIX', JSON.stringify(locations)); 
} 

回答

0

this.addLocation增加了一個位置對象var locations。還應該撥打saveLocations()將這些更改保存到localStorage

1

作爲本單元的講師,我會建議堆棧溢出不是提問任務的最佳位置。你的問題的答案需要從作業指導中獲得知識,這些指導僅供參加單元的學生使用。

此外,您不應該公開您的任何代碼(即解決問題的方法)。作爲提交作業的一部分,您簽署了一份聲明,聲明這是您自己的作品,並且您沒有與任何人分享您的作品。將您的代碼發佈到Stack Overflow可以解決這個問題。不要這樣做!

我建議您仔細閱讀作業指導和作業常見問題解答。如果您仍然有疑問,請在單位論壇上諮詢,詢問您的演示者,或者諮詢或舉辦書桌會議。

在回答你的問題時,saveLocations()應該將LocationWeatherCache實例保存到本地存儲。 addLocation()方法應該爲LocationWeatherCache類的locations數組屬性添加一個新位置,並且(如HotGirlInYourPracDoingENG1003所述)應該調用saveLocations()以確保此更改持續存在。

相關問題