将 .bind (mootools) 与 onClick 事件结合使用

发布于 2024-08-17 12:14:35 字数 797 浏览 5 评论 0原文

我有代码(在一个对象内) <代码> 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:


onclick: this._add.bind(this,'x')
and
onclick: this._add.bind(this,'y')
in the two objects. And of course I changed _add to accept an argument.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开雨落又逢春i 2024-08-24 12:14:35

这是可行的,它将作用域保留在元素单击事件中,并将作用域设置为类而不是元素——将作用域传递给 add 方法是没有意义的,它已经有了:

var foo = new Class({
    Implements: [Options],
    add: function(what) {
        alert(what);
    },
    initialize: function(options) {
        this.setOptions(options);

        this.options.element.addEvents({
            click: function() {
                this.add(this.options.what);
            }.bind(this)
        });
    }
});

window.addEvent("domready", function() {
    new foo({
        element: $("foo"),
        what: "nothin'"
    });
});

只需创建一个 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:

var foo = new Class({
    Implements: [Options],
    add: function(what) {
        alert(what);
    },
    initialize: function(options) {
        this.setOptions(options);

        this.options.element.addEvents({
            click: function() {
                this.add(this.options.what);
            }.bind(this)
        });
    }
});

window.addEvent("domready", function() {
    new foo({
        element: $("foo"),
        what: "nothin'"
    });
});

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/

ぇ气 2024-08-24 12:14:35

如果您阅读了我之前的回答,请忽略它。 MooTools .bind 方法支持传递参数。所以其他东西没有按您期望的那样工作:

onclick: this._add.bind(this, 'y');

这里是 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:

onclick: this._add.bind(this, 'y');

Here is a simple setup on JSBin to show how bind truly does pass parameters.

辞别 2024-08-24 12:14:35

绑定的唯一目的是“告诉”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 to bind an instance of the object you wish the this key word will refer to inside the function you used the bind on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文