Kijimu7
Kijimu7

Reputation: 65

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);

Upvotes: 0

Views: 111

Answers (3)

Charlie
Charlie

Reputation: 23798

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.

Upvotes: 1

Tuhin Das
Tuhin Das

Reputation: 499

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.

Upvotes: 3

Abhishek Anand
Abhishek Anand

Reputation: 477

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

Upvotes: 1

Related Questions