JavaScript MVC 语法
我在这里看到了一篇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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下划线只是约定俗成的,它的意义只是表明某人写下它时脑子里想的是什么。一般来说,人们使用下划线作为私有方法的前缀,这意味着仅在类内部使用,不被其他用户使用。
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.
下划线没有任何意义,您可以在变量名称中使用它。
在这种情况下,它似乎表明它应该用于私有变量。
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.