MW-event advanced application 1
Event binding:
Object. onclick = function () {alert (this) ;}// returns the current object
Object. attachevent ('onclick', function () {alert (this );})
// Return the window object. The bug in IE points to the window object.
Object. addeventlistener ('click', function () {alert (this) ;}, false );
// Returns the current object
Event binding and anonymous functions:
Declare an AAA function first;
Object. attachevent ('onclick', AAA );
Object. detachevent ('onclick', AAA); // unbound
Anonymous functions:
Object. attachevent ('onclick', function () {alert ('A ');});
Object. detachevent ('onclick', function () {alert ('A ');});
// The contact binding fails.
The reason is: each time an anonymous function is used, a new function will be created. Even if the function looks the same, they still have two functions, so the unbinding above is unsuccessful. You can see:
VaR A = function () {alert ('A ');};
VaR B = function () {alert ('A ');};
Alert (A = B); // return false, that is, the two anonymous functions are not the same thing. In fact, two different functions are created, as shown below:
VaR A = new function ('alert ("")');
VaR B = new function ('alert ("")');