Javascript MVC语法

问题描述 投票:3回答:2

[我看到了Javascript MVC文章here,并且模型定义为:

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);
javascript model-view-controller dom-events javascript-framework
2个回答
7
投票

下划线仅是约定俗成的内容,仅具有表示某人写的内容的含义。通常,人们使用下划线为要用作私有方法的方法名称添加前缀,这意味着仅在类内部使用,而其他用户则不使用。


1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.