Whether or not you began with the outdated on_____
property or addEventListener
, you understand that occasions drive consumer experiences in fashionable JavaScript. For those who’ve labored with occasions, you understand that preventDefault()
and stopPropagation()
are incessantly used to deal with occasions. One factor you most likely did not know: there is a defaultPrevented
proptery on occasions!
Think about the next block of code:
// Particular to a hyperlink const hyperlink = doc.querySelector('#my-link'); hyperlink.addEventListener('click on', e => e.preventDefault()); // A bigger doc scope doc.addEventListener('click on', documentClickHandler); operate documentClickHandler(occasion) { if (occasion.defaultPrevented) {// Utilizing the property // Do one factor if the press has been dealt with } else { // In any other case do one thing recent } }
When preventDefault
is named on a given occasion, the defaultPrevented
property will get toggled to true
. Attributable to occasion propagation, the occasion bubbles upward with this defaultPrevented
worth.
I have been dealing with occasions for twenty years and did not know this property existed till now. What’s nice about defaultPrevented
is that it stays with the occasion with no need to trace monitor it globally!
7 Important JavaScript Features
I bear in mind the early days of JavaScript the place you wanted a easy operate for nearly the whole lot as a result of the browser distributors applied options otherwise, and never simply edge options, fundamental options, like
addEventListener
andattachEvent
. Instances have modified however there are nonetheless a couple of capabilities every developer ought to…Create Namespaced Courses with MooTools
MooTools has all the time gotten a little bit of grief for not inherently utilizing and standardizing namespaced-based JavaScript lessons just like the Dojo Toolkit does. Many builders create their lessons as globals which is mostly frowned up. I largely disagree with that stance, however every to their very own. In any occasion…
CSS calc
CSS is a whole conundrum; all of us recognize CSS due to its simplicity however all the time yearn for the language to do only a bit extra. CSS has developed to accommodate placeholders, animations, and even click on occasions. One downside we all the time thought…
Source_link