JavaScript MVC 语法

发布于 2024-10-31 19:51:04 字数 1438 浏览 1 评论 0原文

我在这里看到了一篇Javascript MVC文章,模型是定义为:

var ListModel = function (items) {
    this._items = items;
    this._selectedIndex = -1;

    this.itemAdded = new Event(this);
    this.itemRemoved = new Event(this);
    this.selectedIndexChanged = new Event(this);
};

ListModel.prototype = {

    getItems : function () {
        return [].concat(this._items);
    },

    addItem : function (item) {
        this._items.push(item);
        this.itemAdded.notify({item: item});
    },

    removeItemAt : function (index) {
        var item = this._items[index];
        this._items.splice(index, 1);
        this.itemRemoved.notify({item: item});
        if (index == this._selectedIndex)
            this.setSelectedIndex(-1);
    },

    getSelectedIndex : function () {
        return this._selectedIndex;
    },

    setSelectedIndex : function (index) {
        var previousIndex = this._selectedIndex;
        this._selectedIndex = index;
        this.selectedIndexChanged.notify({previous: previousIndex});
    }

};  

问题 1。在Javascript中,下划线是什么意思?例如this._items

问题2。在模型中,它在哪里使用,如何使用以下内容:

this.itemAdded = new Event(this);
    this.itemRemoved = new Event(this);
    this.selectedIndexChanged = new Event(this);

I saw a Javascript MVC article here, and the model is defined as:

var ListModel = function (items) {
    this._items = items;
    this._selectedIndex = -1;

    this.itemAdded = new Event(this);
    this.itemRemoved = new Event(this);
    this.selectedIndexChanged = new Event(this);
};

ListModel.prototype = {

    getItems : function () {
        return [].concat(this._items);
    },

    addItem : function (item) {
        this._items.push(item);
        this.itemAdded.notify({item: item});
    },

    removeItemAt : function (index) {
        var item = this._items[index];
        this._items.splice(index, 1);
        this.itemRemoved.notify({item: item});
        if (index == this._selectedIndex)
            this.setSelectedIndex(-1);
    },

    getSelectedIndex : function () {
        return this._selectedIndex;
    },

    setSelectedIndex : function (index) {
        var previousIndex = this._selectedIndex;
        this._selectedIndex = index;
        this.selectedIndexChanged.notify({previous: previousIndex});
    }

};  

question 1. In Javascript, what does underscore means? e.g. this._items

question 2. in model, where does it use, how to use the following things:

this.itemAdded = new Event(this);
    this.itemRemoved = new Event(this);
    this.selectedIndexChanged = new Event(this);

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

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

发布评论

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

评论(2

一个人的夜不怕黑 2024-11-07 19:51:04

下划线只是约定俗成的,它的意义只是表明某人写下它时脑子里想的是什么。一般来说,人们使用下划线作为私有方法的前缀,这意味着仅在类内部使用,不被其他用户使用。

The underscore is just convention, it only has meaning to indicate what was in somebodies head when they wrote it. In general people use an underscore to prefix method names that are meant to be private methods, meaning only use internally to a class, not used by other users.

浅唱々樱花落 2024-11-07 19:51:04

下划线没有任何意义,您可以在变量名称中使用它。

在这种情况下,它似乎表明它应该用于私有变量。

The underscore doesnt mean anything, you can just use it in your variable names.

In this case it seems to be an indication that its supposed to be used for a private variable.

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