【发布时间】:2014-08-26 21:05:54
【问题描述】:
在Norway this yearDoug Crawford 宣布他不再在 JavaScript 中使用this(或者他的意思是 ES6?)。真诚的,这里怎么不使用this:
var indexViewModel = {
currentIndex: ko.observable(0),
currentPage: ko.observable(1),
displayItems: ko.observableArray(),
indexSegmentsForPeople: [
{ id: "poetryinxhtml", text: "Poetry in XHTML5" },
{ id: "poetryinpdf", text: "Poetry in PDF" },
{ id: "poetryinstreams", text: "Poetry in Streams" },
{ id: "digitizedart", text: "Digitized Art" }
],
indexSegmentsForTime: [
{ id: "prose", text: "Prose" },
{ id: "interviewsanddocumentary", text: "Interviews/Documentary" },
{ id: "therasxcontext", text: "the rasx() context" }
],
indexVisitorLinks: [
{ url: "./rasx01.html", title: "about the kinté space" },
{ url: "./kfaqs.html", title: "kinté FAQs" },
{ url: "./khits.html", title: "kinté hits" },
{ url: "./rasx00.html", title: "the kinté space oddity" }
],
items: [],
itemsCount: ko.observable(0),
itemsPerPage: 9,
pageCount: ko.observable(0),
getCurrentIndex: function () {
return ((this.currentPage() - 1) * this.itemsPerPage);
},
getJSON: function (id) {
var url = this.getIndexJsonUrl(id);
var that = this;
return $.getJSON(url).then(function (data) {
that.setItems(data);
});
},
getJSONforVisitors: function () {
var that = this;
return $.getJSON('./data/VisitorThumbs.json').then(function (data) {
that.setItemsForVisitors(data);
});
},
getIndexJsonUrl: function (id) {
return './data/index-' + id + '.json';
},
getIsNewValue: function (pubDate) {
var now = new Date();
var test1 = ((now.getMonth() - pubDate.getMonth()) <= 2);
var test2 = (pubDate.getFullYear() === now.getFullYear());
return (test1 && test2);
},
getSlice: function () {
return this.items.slice(this.currentIndex(),
(this.currentIndex() + this.itemsPerPage));
},
isNextPageEnabled: ko.observable(true),
isPreviousPageEnabled: ko.observable(false),
setDisplayItems: function () {
var that = this;
var slice = this.getSlice();
this.displayItems.remove(function (item) { return item; });
_(slice).each(function (item) { that.displayItems.push(item); });
},
setEnabled: function () {
this.isNextPageEnabled(this.currentPage() < this.pageCount());
this.isPreviousPageEnabled(this.currentPage() > 1);
},
setItems: function (data) {
var that = this;
this.items = _(data.ChildDocuments)
.sortBy(function (d) { return $.rx.dateTime.getJsonDate(d.CreateDate); }).reverse();
_(this.items).each(function (item) {
//Add .isNew property:
var pubDate = $.rx.dateTime.getJsonDate(item.CreateDate);
item.isNew = that.getIsNewValue(pubDate);
});
this.currentIndex(0);
this.currentPage(1);
this.itemsCount(this.items.length);
this.pageCount(Math.ceil(this.itemsCount() / this.itemsPerPage));
this.setDisplayItems();
this.setEnabled();
},
setItemsForVisitors: function (data) {
this.items = data.ChildDocuments;
this.currentIndex(0);
this.currentPage(1);
this.itemsCount(this.items.length);
this.pageCount(Math.ceil(this.itemsCount() / this.itemsPerPage));
this.setDisplayItems();
this.setEnabled();
},
setNextPage: function () {
this.currentPage(this.currentPage() + 1);
this.setEnabled();
if (this.currentPage() > this.pageCount()) { this.currentPage(this.pageCount()); }
this.currentIndex(this.getCurrentIndex());
this.setDisplayItems();
},
setNextPageClickGate: function () {
if (this.isNextPageEnabled()) { this.setNextPage(); } else { return false; }
},
setPreviousPage: function () {
this.currentPage(this.currentPage() - 1);
this.setEnabled();
if (this.currentPage() < 1) { this.currentPage(1); }
this.currentIndex(this.getCurrentIndex());
this.setDisplayItems();
},
setPreviousPageClickGate: function () {
if (this.isPreviousPageEnabled()) { this.setPreviousPage(); } else { return false; }
},
triggerAfterAdd: function (item, iterator) {
$(window.document).trigger('afterAdd',
['IndexFlowTemplate', item, iterator]);
}
};
???
【问题讨论】:
-
不确定他的意思,但你可以在任何你使用
this的地方使用indexViewModel。 -
也许他的意思是他不使用原型继承。我不确定很多人是否愿意观看一个小时长的视频,只是为了抓住他发表该声明的要点,因此如果您注意视频中的相关时间戳,将会有很大帮助。
-
克罗克福德说了很多我觉得很傻的话。
-
我同意饼干怪物。我以前说过,我再说一遍:Crockford 确实推动了有关 JavaScript 的讨论,但并不是所有从他嘴里说出来的东西都是金子。
-
我也同意 - Crockford 有很多非常有效的观点,但有时似乎将他的个人风格与绝对正确性混淆了。一个很好的例子是他从不希望您使用 ++,因为较少的程序员可能不理解运算符。愤世嫉俗和愚蠢。