Is there a bug in this code example from "Beginning JavaScript with DOM Scripting and Ajax" or I am missing something?

StackOverflow https://stackoverflow.com/questions/21404403

Question

I'm reading a book called "Beginning JavaScript with DOM Scripting and Ajax" (2006)

On Page 168/139 it offers some example code, a stopBubble function, which promises to prevent an event from propagating in both IE and DOM-2 compliant browsers:

stopBubble:function(e){
  if(window.event && window.event.cancelBubble){
    window.event.cancelBubble = true;
  }
  if (e && e.stopPropagation){
    e.stopPropagation();
  }
}

Since the cancelBubble property is a boolean property that defaults to false, I am wondering how we will ever reach the line where cancelBubble is set to true without it already being true. In other words that condition seems pointless.

I would not call myself an expert on JavaScript and so am wondering if there is something that I am missing (perhaps a hack utilizing some aspect of JavaScript's unintuitive truthiness). There is no errata logged for this in the book's Errata page. I suspect this is a mistake - but not 100% sure. Is it a broken example?

Était-ce utile?

La solution

Seems you are correct and should notify the author

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top