Javascript 这是什么意思?这个[';find';](函数(模型){…})
我试图理解主干集合Javascript 这是什么意思?这个[';find';](函数(模型){…}),javascript,backbone.js,Javascript,Backbone.js,我试图理解主干集合findWhere()函数是如何工作的,我在代码中看到了这一点: // Return models with matching attributes. Useful for simple cases of // `filter`. where: function(attrs, first) { var matches = _.matches(attrs); return this[first ? 'find' : 'filter'
findWhere()
函数是如何工作的,我在代码中看到了这一点:
// Return models with matching attributes. Useful for simple cases of
// `filter`.
where: function(attrs, first) {
var matches = _.matches(attrs);
return this[first ? 'find' : 'filter'](function(model) {
return matches(model.attributes);
});
},
// Return the first model with matching attributes. Useful for simple cases
// of `find`.
findWhere: function(attrs) {
return this.where(attrs, true);
},
我试图理解这部分的作用:
return this[first ? 'find' : 'filter'](function(model) {
return matches(model.attributes);
});
这部分这个['find'](函数(模型){…})
实际上是做什么的?它是这样的
是一个此
对象
检查bool[first'find':'filter']
是否为正数,然后返回first
否则'find'
这是通过括号符号访问的函数引用'filter'
。简而言之,使用三元运算符通过bracker表示法访问函数引用[]
是对该函数的调用李>(…){}
- 事情是这样的
是一个此
对象
检查bool[first'find':'filter']
是否为正数,然后返回first
否则'find'
这是通过括号符号访问的函数引用'filter'
。简而言之,使用三元运算符通过bracker表示法访问函数引用[]
是对该函数的调用李>(…){}
所以答案是:它是对象的方法this.find(function(model){ ... })
,这个是用参数调用的(我假设它是回调:)find
这部分等于:函数(模型){…}
this.find(function(model){ ... })
所以答案是:这是对象的方法
,这个是用参数调用的(我假设它是回调:)find
,在javascript中,你可以用括号符号代替点符号,括号符号在你正在使用的这种情况下非常方便。因此,以下内容是相同的:函数(模型){…}
foo.['bar'] foo.bar()
在这方面:return this[first ? 'find' : 'filter'](function(model) {
如果第一个值返回true,则将使用
,否则将使用this.find()
。您可以在javascript中使用括号表示法而不是点表示法,括号表示法在这样的情况下非常方便。因此,以下内容是相同的:this.filter()
foo.['bar'] foo.bar()
在这方面:
如果第一个值返回true,则将使用this.find(),否则将使用this.filter()。return this[first ? 'find' : 'filter'](function(model) {
与return this[first ? 'find' : 'filter'](function(model) { return matches(model.attributes); });
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
这和if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
这和if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
这和if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
我希望这能把事情弄清楚。这if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
与return this[first ? 'find' : 'filter'](function(model) { return matches(model.attributes); });
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
这和if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
这和if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
这和if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }
var functionName = (first ? 'find' : 'filter'); return this[functionName](function(model) { return matches(model.attributes); });
var functionName; if(first){ functionName = 'find'; } else { functionName = 'filter'; } return this[functionName](function(model) { return matches(model.attributes); });
if(first){ return this['find'](function(model) { return matches(model.attributes); }); } else { return this['filter'](function(model) { return matches(model.attributes); }); }
我希望这能把事情弄清楚if(first){ return this.find(function(model) { return matches(model.attributes); }); } else { return this.filter(function(model) { return matches(model.attributes); }); }