【问题标题】:Caches! What is the difference between the saveLocations() and addLocation() function?缓存! saveLocations() 和 addLocation() 函数有什么区别?
【发布时间】:2016-09-10 12:51:43
【问题描述】:

我是 Javascript 的初学者,我被分配了一项任务。它基本上是一个天气应用程序.. 我创建了一个页面,该页面将基于谷歌地理定位 API 输出一个地方的纬度、经度和昵称。现在我想打电话给 forecast.io 把结果给我。 我应该做的是在 LocalStorage 中存储纬度、经度和昵称......当单击“保存位置”按钮并将所有位置保存到列表中,以便他们可以单击并获取天气信息。 但是后来我得到了一个框架代码,我不知道它是做什么的。 我在后面写的 this.AddLocation 和 savelocation() 函数有什么区别。 我在这里编写的唯一函数是将位置保存到本地存储的 savelocation() 函数。其他功能为骨架代码,需要填写。

对类中的方法应该做什么的任何解释都会有很大帮助!

代码如下:

// 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));
}

【问题讨论】:

    标签: javascript caching local-storage forecasting weather


    【解决方案1】:

    作为本单元的讲师,我建议 Stack Overflow 不是就作业提问的最佳场所。您的问题的答案需要作业说明中的知识,这些知识仅适用于参加该单元的学生。

    此外,您不应公开发布任何代码(即您对问题的解决方案)。作为提交作业的一部分,您签署一份声明,说明这是您自己的工作,并且您没有与任何人分享您的工作。将您的代码发布到 Stack Overflow 会破坏这一点。不要这样做!

    我建议您仔细阅读作业说明和作业常见问题解答。如果您仍有疑问,请在单元论坛上提问,询问您的演示者,或在咨询或举行的桌面会议中提问。

    在回答您的问题时,saveLocations() 应将LocationWeatherCache 实例保存到本地存储。 addLocation() 方法应该向 LocationWeatherCache 类的 locations 数组属性添加一个新位置,并且(正如 HotGirlInYourPracDoingENG1003 所说)它应该调用 saveLocations() 以确保保持此更改。

    【讨论】:

      【解决方案2】:

      this.addLocation 将位置对象添加到var locations。它还应该调用saveLocations() 将这些更改保存到localStorage

      【讨论】:

        猜你喜欢
        • 2011-04-11
        • 2021-04-28
        • 2017-07-15
        • 2015-12-16
        • 2020-05-30
        • 2011-09-22
        • 2015-03-11
        • 2015-01-08
        • 2017-11-24
        相关资源
        最近更新 更多