About event delegation and time bubbling (and matters of apply and call)

Source: Internet
Author: User

Search event delegation and event bubbling, a lot of messy explanations on the Internet, of course, all right, no problem.

But it's really boring.

Event bubbling: The event will flow up and down from the clicked element until the HTML, the event listener is executed.

Event delegate: Reason-There are several indeterminate child elements under the parent element that require the same listener event to be added. Processing method-Using event bubbling property to listen to the parent element and then use E.target to get the clicked child element, the event source.

Then, when it comes to event delegation, we talk about the apply and call methods.

Apply method, there is a lot of explanations on the Internet, the old look, I still like the simple point.

1. The so-called apply is to change the current object of this, so as to achieve the effect of class inheritance.

The 2.js function has the concept of "context of definition", "Runtime Context" and "context can change", so there are a lot of weird things, and that's one of them.

3. The context is this.

The following is the answer of the great God, I understand the note in this, thank you.

First, everything in JS is object. The object's method thousands, if one object exists a method or property that another object does not exist, it can be inherited by the apply or call method.

For example, the nodes obtained by means of var nodes = document.getElementsByTagName is an array of class arrays, i.e. We can go through

Array.prototype.push.apply (nodes) allows nodes to use the push method of the array.

Apply and call function exactly the same, except that the receiving parameters are different:

Apply (this, arg1,agr2 ... );

Call (This,[arg1,arg2 ...] );

Here is an example:

functionMan () {}man.prototype={valet:false, WakeUp:function(event) {Console.log ( This. Valet + "? Some breakfase, please. "); }};//get "undefined?" Some Breakfast, pleasevarbutton = document.getElementById (' Morning '); Button.addeventlistener ("Click", Wooster.wakeup,false);//use Apply to change the context of the wakeUp, which is the this in WakeUpvarbutton = document.getElementById (' morning2 '); Button.addeventlistener ("Click",    function() {Man.prototype.wakeUp.apply (Wooster, arguments); },    false);

Actually. Apply and. Call are just two special methods of function. The effect is to change the execution environment of the function-that is, the context-that is, to see an example of a straight view.

function Changestyle (attr, value) {    this. style[attr] = value;}  var box = document.getElementById (' box '"height", "200px");

We define a function changestyle under window and get to box. box is a node, there is no Changestyle method in itself, we use the. Call to work with the Changestyle method under window to solve it perfectly.

About event delegation and time bubbling (and matters of apply and call)

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.