将 .bind (mootools) 与 onClick 事件结合使用
我有代码(在一个对象内) <代码> onclick: this._addX.bind(this) 然后在另一个对象内 <代码> onclick: this._addY.bind(this) y
现在,_addX() 和 _addY 几乎相同,只是它们最终都调用(在单击事件上)具有不同参数值的函数,例如 _addX 调用 foo('x') 和 _addY 调用 foo(' ')。所以我尝试了:
<代码> onclick: this._add.bind(this,'x') 和 onclick: this._add.bind(this,'y')
在两个对象中。当然,我更改了 _add 以接受参数。
在运行时,当调用 _add 时,它看不到任何传入参数!我摸索过不同的语法,但没有任何效果。有什么想法吗?原始语法工作正常(没有参数),但迫使我复制一个只有一行不同的大函数,这让我很痛苦。提前致谢。
_add: function(which) {
var me = this;
var checkFull = function(abk) {
if (abk.isFull) {
alert("full");
} else {
alert(which); // which is always undefined here!
}
};
getAddressBook(checkFull); //checkFull is a fn called by getAddressBook
},
I have the code (inside one object)
onclick: this._addX.bind(this)
and then inside another object
onclick: this._addY.bind(this)
Now, _addX() and _addY are nearly identical, except they both end up calling (on the click event) a function with different argument values, say _addX calls foo('x') and _addY calls foo('y'). So I tried:
and
onclick: this._add.bind(this,'x')
in the two objects. And of course I changed _add to accept an argument.
onclick: this._add.bind(this,'y')
At runtime, when _add is called, it does not see any incoming arguments! I have fumbled around with different syntaxes but nothing works. Any ideas? The original syntax works fine (no arguments) but forces me to duplicate a large function with only one line different, which pains me. Thanks in advance.
_add: function(which) {
var me = this;
var checkFull = function(abk) {
if (abk.isFull) {
alert("full");
} else {
alert(which); // which is always undefined here!
}
};
getAddressBook(checkFull); //checkFull is a fn called by getAddressBook
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是可行的,它将作用域保留在元素单击事件中,并将作用域设置为类而不是元素——将作用域传递给 add 方法是没有意义的,它已经有了:
只需创建一个 id=foo 的元素并单击它进行测试(没有任何警报)。如果您的 onclick 是类中的函数/事件处理程序而不是普通的元素单击事件,那么事情会略有不同 - 在 http://mootools.net/shell/
this works and it keeps the scope within an element click event with the scope set to the class and not the element--there is no point in passing scope to the add method, it already has that:
just make an element with id=foo and click it to test (alerts nothin'). if your onclick is a function / event handler within your class as opposed to a normal element click event, then things are going to differ slightly - post a working skeleton of your work on http://mootools.net/shell/
如果您阅读了我之前的回答,请忽略它。 MooTools
.bind
方法支持传递参数。所以其他东西没有按您期望的那样工作:这里是 JSBin 上的简单设置,以展示 Bind 的真正作用传递参数。
If you read my previous answer, disregard it. The MooTools
.bind
method supports passing parameters. So something else isn't working as you expect:Here is a simple setup on JSBin to show how bind truly does pass parameters.
绑定的唯一目的是“告诉”JS,当你说
this
时,你指的是什么对象。即,您将您希望this
关键字在您使用bind
的函数内引用的对象实例作为参数传递给bind
在。The only purpose of bind is to "tell" the JS what object you mean when you say
this
. i.e. you pass as a parameter tobind
an instance of the object you wish thethis
key word will refer to inside the function you used thebind
on.