&是什么QUOT; this._events || (this._events = {});"意思? [英] What does "this._events || (this._events = {});" mean?
问题描述
我已经开始学习Backbone.js的。目前我的JavaScript技能并不太好。我已经开始检查 Backbone.js的
文件和所遇到的code,其目的我不能想出了一个奇怪的线。 code样本(如果你需要更多的情况下,手动下载 Backbone.js的为开发者和看行80 ):
I have started learning Backbone.js. Currently my JavaScript skills are not too good. I have started to examine the backbone.js
file and have come across a strange line of code whose purpose I can not figure out. Code sample (if you need more context, manually download backbone.js for developers and see line 80):
var Events = Backbone.Events = {
// Bind an event to a `callback` function. Passing `"all"` will bind
// the callback to all events fired.
on: function(name, callback, context) {
if (!eventsApi(this, 'on', name, [callback, context]) || !callback) return this;
this._events || (this._events = {});
var events = this._events[name] || (this._events[name] = []);
events.push({callback: callback, context: context, ctx: context || this});
return this;
},
这是什么行 this._events || (this._events = {});
是什么意思?对我来说, _events
看起来像一个内部变量,但(this._events = {})
用于转让或它是一个或比较呢?或者是 ||
在这种情况下完全不同的操作?
What does the line this._events || (this._events = {});
mean? For me, _events
looks like an inner variable, but is (this._events = {})
used for assignment or is it an or comparison? Or is ||
a completely different operation in this context?
推荐答案
这是一个使用JavaScript的falsy的评价一招。它是一样的:
It is a trick that uses javascripts "falsy" evaluation. It is the same as:
if (this._events) {
// do nothing, this._events is already defined
} else {
this._events = {};
}
这同样适用于行 VAR的事件= this._events [名] || (this._events [名] = []);
这可以转换为
var events;
if (this._events[name]) {
events = this._events[name];
} else {
this._events[name] = [];
events = this._events[name];
}
这篇关于&是什么QUOT; this._events || (this._events = {});"意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!