避免 Mootools 中的匿名函数?

发布于 2024-10-15 03:03:44 字数 589 浏览 1 评论 0原文

这是我的问题:我正在寻找一种在 Mootools 中使用“非匿名”功能的方法。 找不到解决方法,也找不到有关它的文档。

示例: var myInput = new Element('输入', { 类型:'文本', 值:'你好世界', 事件:{模糊:函数(){ .... } }; });

$$('td[title]').addEvents('dblclick':function(){...});

和一个简单函数 function sayHello(){alert('hello')}

如果我希望 onBlur/ondblClick 事件调用现有函数(例如 sayHello),我该怎么办?我知道我可以在匿名函数中调用现有函数,但是有没有更好/合适的方法来执行该调用? 我被这个问题困扰了几天,也许它很简单,但我就是无法弄清楚。

谢谢。

编辑 : 我怎么会错过呢???谢谢你们的帮助,工作得很好!

Here's my problem: I'm looking for a way to work with 'not anonymous' function in Mootools.
Can't find a way to work it out, and I couldn't find doc about it.

Example:
var myInput = new Element('input', {
type: 'text',
value: 'hello world',
events: {blur: function(){ .... }
};
});

OR
$$('td[title]').addEvents('dblclick':function(){...});

AND a Simple function
function sayHello(){alert('hello')}

What should I do if I want onBlur/ondblClick event calls an existing function (eg. sayHello) ? I know I could call my existing function inside the anonymous one, but is there any better/suitable way to do that call ?
I'm stuck on that problem for a few days, maybe it's quite simple, but I simply can't figure it out.

Thank you.

EDIT :
How could I missed that ??? Thank you guys for the help, worked great !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

桃扇骨 2024-10-22 03:03:44
$('td[title]').addEvents({'dblclick':sayHello});

因为函数是一个变量,所以您可以像使用变量一样使用它,在本例中作为哈希值,

我还应该指出,这两个函数将具有相同的名称:

function sayHello(){}

and

var sayHello = function(){}

它们都创建一个名为 ' sayHello'(有一些你现在不关心的细微差别)

$('td[title]').addEvents({'dblclick':sayHello});

because a function is a variable, you can use it as you would use a variable, in this case as a value of a hash

also i should point out that both of these functions will have the same name:

function sayHello(){}

and

var sayHello = function(){}

they both make a function called 'sayHello' (with subtle differences that you dont care about right now)

多情出卖 2024-10-22 03:03:44

函数是 Javascript 中的一等公民,因此您可以像这样引用它们:

$('td[title]').addEvents('dblclick':sayHello);

Functions are first class citizens in Javascript, so you can reference them like this:

$('td[title]').addEvents('dblclick':sayHello);
夜无邪 2024-10-22 03:03:44

只需直接传递对函数的引用,就像它是任何其他变量一样:

var myInput = new Element('input', {
    type: 'text',
    value: 'hello world',
    events: {blur: sayHello}
});

Simply pass in a reference to your function directly, as if it were any other variable:

var myInput = new Element('input', {
    type: 'text',
    value: 'hello world',
    events: {blur: sayHello}
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文