将名称数组传递给 _.bindAll - 参数列表的类型错误
passing an array of names to _.bindAll - Arguments list has wrong type
我们在主干视图中使用通用模式。我们的事件对象如下所示:
var TokenInputBaseView = Backbone.View.extend({
events: {
'click .remove-token' : 'click_removeToken',
'mousedown .add-token': 'click_addToken',
'keydown input' : 'keydown_input',
'click input' : 'click_input',
'focus .token-input' : 'focus_input',
'blur .token-input' : 'blur_input',
},
在几乎每种情况下,我们都希望所有事件处理程序都绑定到 View,而不是绑定到它们的事件对象。所以我们这样做:
initialize: function(){
_.bindAll(this, 'click_removeToken', ...);
}
我们必须手动列出每个事件名称。如果我们可以简单地传入一个数组,那将是理想的,但下划线不允许这种使用:
_.bindAll(this, _.values(this.events));
下划线希望将各个名称作为参数而不是数组传入。但是,这也不起作用:
_.bindAll.apply(this, _.values(this.events).unshift(this));
Javascript给出了这个错误:
"Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type"
关于简化 bindAll 使用的好方法的任何想法,以便它不需要手动列出要绑定的所有函数名称?
您正在传递 bindAll unshift 的返回,这是修改后的数组的长度。您需要存储数组引用,对其进行修改,然后将该引用传递给apply
或使用其他一些技巧:
// note, no need to bind to `this`
_.bindAll.apply(null, [this].concat(_.values(this.events)));
举个小例子:
var target = document.querySelector('#target');
var map = {
'foo': 'foo',
'bar': 'bar'
};
function C() {
this.value = 'Value';
_.bindAll.apply(null, [this].concat(_.values(map)));
}
C.prototype.foo = function() {
console.log('hello', this);
target.textContent = this.value + ' set by foo';
}
C.prototype.bar = function() {
return this.value;
}
var c = new C();
document.addEventListener('click', c.foo);
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<div id="target">Click me</div>
相关文章:
- 参数列表Three.js之后的未捕获语法错误:缺少)
- 是否可以在javascript函数参数列表中调用多个函数
- 参数列表(节点)后缺少)
- 从从字符串创建的函数中获取参数列表
- 参数列表后缺少未捕获的语法错误)
- 参数列表Javascript Jquery后缺少)
- 在带有参数列表的表单submit上调用一个方法
- 参数列表后的未捕获语法错误:缺少)
- 参数列表[Javascript语法错误]后的未捕获语法错误:缺少)
- 删除元素并将其添加到 javascript 中的函数参数列表中
- 缺少)在参数列表之后,这次我把它放在哪里
- Javascript ( jQuery ) 错误: 语法错误: 缺少 ) 在参数列表之后
- Function.prototype.apply:参数列表的类型错误
- 语法错误:缺少参数列表函数 () {.
- jQuery:缺少参数列表之后的 )
- 缺少 ) 在参数列表之后.我做错了什么
- Nodejs pdf2json 缺少 ) 在参数列表之后
- 将数组作为参数列表传递给具有现有参数的函数
- 缺少 ) 在 SFDC 中的参数列表之后
- 将名称数组传递给 _.bindAll - 参数列表的类型错误