简体   繁体   中英

What is the 0 indicate at end of the function in this code?

What is the 0 define at the end of each function in this code? Why this code set 0s at the end of the function?

 canvas.addEventListener('mousedown', function(e) { model.dragging = getCircleForPosition(e.pageX, e.pageY); }, 0); canvas.addEventListener('mouseup', function() { model.dragging = undefined; }, 0); canvas.addEventListener('mousemove', function(e) { if (model.dragging) { model.dragging.x = e.pageX; model.dragging.y = e.pageY; redraw(); } }, 0); 

In addEventListener you can basically pass three arguments event, callback and the third one is optional which takes a boolean true for enabling event capturing and false for enabling event bubbling. Here 0 will considered as false hence it will enable event bubbling on the events.

The 0 basically indicates a false value. According to docs

true - The event handler is executed in the capturing phase

false- Default. The event handler is executed in the bubbling phase

The third argument of the addEventListner function can ether be the options object or a boolean value indicating the capturing mode.

useCapture (Optional) -- A Boolean indicating whether events of this type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. Events that are bubbling upward through the tree will not trigger a listener designated to use capture. Event bubbling and capturing are two ways of propagating events which occur in an element that is nested within another element, when both elements have registered a handle for that event. The event propagation mode determines the order in which elements receive the event. See DOM Level 3 Events and JavaScript Event order for a detailed explanation. If not specified, useCapture defaults to false.

If your 0 value is interprets in a boolean context, it is to tell the even listener to not use the capture - which is the default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM