2

关于传递给在单击事件时调用的函数的参数,我对 Firefox 4 的行为有疑问。

看看这个例子:

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.11/mootools.js"></script>
</head>
<body>
    <span id="e">Klick mich!</span> 
    <script type="text/javascript">
        $("e").addEvent("click", function(a, b, c){
            alert(this);
            alert(a);
            alert(b);
            alert(c);
            console.log(this);
            console.log(a);
            console.log(b);
            console.log(c);
        }.bind(1, [2, 3]));
    </script>   
</body>
</html>

如果你用 Firefox 4 打开它,结果是:

  1. 1
  2. 2,3
  3. 对象鼠标事件
  4. 不明确的

在任何其他浏览器中,结果是:

  1. 1
  2. 2
  3. 3
  4. 不明确的

如您所见,只有 Firefox 4 将 MouseEvent 传递给该函数。这种行为破坏了我的很多代码。

你知道有什么解决办法吗?感谢帮助。

EDIT1:Chrome 的行为类似于 FF4

4

1 回答 1

1

问题是 - 这是 mootools 1.11 -不支持和旧

在 mootools 1.11 中,它被接受使用(http://docs111.mootools.net/Native/Function.js#Function.bind):

bind 可选,函数的“this”将引用的对象。

args 可选,传递的参数。如果 arguments > 1 则必须是数组

因此,做 .bind(1, [args])was正确。然而,最近原生Function.bind实现在实现它的浏览器中发生了变化 - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

thisArg 调用绑定函数时作为 this 参数传递给目标函数的值。如果绑定函数是使用 new 运算符构造的,则忽略该值。

arg1, arg2, ... 在调用目标函数时添加到提供给绑定函数的参数的参数。

这意味着,要使其工作,您需要.bind(1,2,3,4);1 是绑定范围,而 2,3,4 是参数。

应该升级,在它编写 4 年后在浏览器上运行 mootools 1.11 会产生不可预测的结果。总是。例如,1.11 将不再检测 gecko/ff,因为用于测试的 func 已弃用。

接下来,检查netscape4?:)

于 2011-06-03T12:17:40.573 回答