关于Javascript模型构建的问题

发布于 2024-11-03 18:17:04 字数 1029 浏览 1 评论 0原文

我正在致力于在我正在开发的网络应用程序上实现 Javascript 模型。该模型的目的是简单地保存有关页面状态的信息。我遇到过两种不同的创建模型的实现,我想知道哪一种最好用。第一个实现:

var PageInfo = function () {
    this._info = {}; 
};

PageInfo.prototype = {
    getInfo: function () {
        return this._info;
    },
    setInfo: function (updatedInfo) {
        this._info = updatedInfo;
    }
};

第二个实现:

var pageInfo = function () {
    var info = {};

    return {
        getInfo: function () {
            return info;
        },
        setInfo: function (updatedInfo) {
            info = updatedInfo;
        }
    }
};

我的另一个问题是关于 setInfo() 函数。当我发现自己更新模型时,我经常希望立即获得刚刚更改的信息。这导致我这样编写 setter 函数:

setInfo: function(updatedInfo) {
    info = updatedInfo;
    return info;
}

我在代码中实现它,如下所示:

var info = pageInfo.setInfo(newInfo);

这样可以吗还是我应该像这样实现它?:

pageInfo.setInfo(newInfo);
var info = pageInfo.getInfo();

只是尝试遵循最佳实践并避免使用可能出现的任何问题错误的实施。

I'm working on implementing a Javascript model on a web app that I'm working on. The purpose of the model is to simply hold information about the state of the page. I have come across two different implementations for creating the model and I was wondering which one was the best to use. The first implementation:

var PageInfo = function () {
    this._info = {}; 
};

PageInfo.prototype = {
    getInfo: function () {
        return this._info;
    },
    setInfo: function (updatedInfo) {
        this._info = updatedInfo;
    }
};

The 2nd implementation:

var pageInfo = function () {
    var info = {};

    return {
        getInfo: function () {
            return info;
        },
        setInfo: function (updatedInfo) {
            info = updatedInfo;
        }
    }
};

Another question I have is about the setInfo() function. When I find myself updating the model, I often want to have the info that I just changed immediately available to me. This has led me to write the setter function as such:

setInfo: function(updatedInfo) {
    info = updatedInfo;
    return info;
}

which I implement in the code like so:

var info = pageInfo.setInfo(newInfo);

Is this ok or should I be implementing it like this?:

pageInfo.setInfo(newInfo);
var info = pageInfo.getInfo();

Just trying to follow best practices and avoid any issues that may come up from using the wrong implementation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-11-10 18:17:04

两种实现方式都是完全可以接受的。前者会更快,尽管速度可以忽略不计,但会将其内部结构暴露给世界。这意味着任何持有 PageInfo 实例引用的变量都能够操作 _info 属性。一般来说,这不是问题,因为带有下划线 (_) 的属性被认为是私有的,应该单独保留。

后一种实现利用了称为闭包的概念。当返回一个维护对其定义范围的引用的值时,就形成了闭包。本质上,这意味着 info 变量保持活动状态。由于变量是“封闭的”,因此不可能在您提供的接口方法之外对其进行修改。根据我的经验,很少需要这样做。

关于你的第二个问题,同样可以接受。我喜欢访问器方法的接口保持一致,因此我想说 setInfo 不应返回任何内容,但每个规则都有例外。当 getInfo 返回的值与传递给 setInfo 的值不一致时,就会出现该规则的例外情况。也就是说,如果 getInfo 返回的值与传递给 setInfo 的值不同,您可能希望从 setValue 返回值。否则,调用者可以简单地重新使用传递给 setValue 的值,而不是调用 getValue

Both implementations are perfectly acceptable. The former will be faster, albeit by a negligible amount, but exposes its internals to the world. This means that any variable that holds a reference to an instance of PageInfo will be able to manipulate the _info property. Generally this is a non-issue as properties that being with an underscore (_) are considered to be private and should be left alone.

The latter implementation makes use of a concept called closure. A closure is formed when a value is returned that maintains a reference to its defining scope. Essentially it means that the info variable is kept alive. Since the variable is "closed over" it's impossible for it to be modified outside of the interface methods that you provide. In my experience this is rarely needed.

In regards to your second question, again either is acceptable. I like the interface for accessor methods to be consistent so I would say that setInfo should not return anything but there are exceptions to every rule. The exception to the rule occurs when the the value returned by getInfo will not be congruent with what was passed to setInfo. That is, if getInfo returns different value than the value passed to setInfo you might want to return the value out of setValue. Otherwise, the caller could simply re-use the value that was passed to setValue instead of calling getValue.

柏林苍穹下 2024-11-10 18:17:04

第一个实现更好,因为在第二个实现中,每次调用 pageInfo 函数时,您都定义了 2 个新函数(或设置了一个闭包),而第一个实现是在原型中定义它们并重用。

对于其他问题,如果您这样做只是为了保留行,我可能会建议类似:

var info = pageInfo.setInfo(newInfo) || pageInfo.getInfo();

假设 setInfo 返回未定义。

1st implementation is better, as in 2nd implementation every time you call pageInfo function, you are defining 2 new functions (or set up a closure), while 1st implementation is defining them in prototype and reusing.

For other question, if you only do this to preserve lines, i might suggest something like:

var info = pageInfo.setInfo(newInfo) || pageInfo.getInfo();

assuming setInfo returns undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文