The difference between bindwithevent and Bindaseventlistener in MooTools

Source: Internet
Author: User
Tags closure mootools

Bindaseventlistener is a method of the function class in MT, which is to create a closure and automatically pass in an event instance, which is usually used to do the incident listener

Its return value is: A function closure that binds this and the prefetch parameter event (or window.event, different depending on the browser).

Note that the event is just an ordinary event instance, not an instance of the event class in MT, so it does not have properties in the event class, such as event.page.x, and so on.

Bindasevent is a prototype method that the event class in Mt adds to the Functionle class. The effect is that an MooTools event class instance is automatically passed in to the target function.

Here is an example of the difference between the two:

1.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. 3. 4. <title>无标题页</title>
5. <script type="text/javascript" src="mt.js"></script>
6. 7.<body>
8.
9.<a id="demo" style="width:100px;height:50px;border:1px solid red;display:block" href="http://www.google.cn"></a>
10.
11.<script type="text/javascript">
12.var myElement = $('demo');
13.14.function myFunction(event){
15. //var event = new Event(event);
16. //event.stop();
17.
18. alert(event.client.x);
19.};
20.myElement.onclick = myFunction.bindAsEventListener(myElement); //报错
21.myElement.addEvent('click', myFunction.bindWithEvent(myElement)); //正常
22.</script>
23.</body>
24.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.