'Is there a bug in this code example from "Beginning JavaScript with DOM Scripting and Ajax" or I am missing something?
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?
Solution 1:[1]
Seems you are correct and should notify the author
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | in need of help |