It is uncommon that I am disenchanted by the JavaScript language not having a operate that I would like. One such case was summing an array of numbers — I used to be anticipating Math.sum
or a likewise, baked in API. Concern not — summing an array of numbers is straightforward utilizing Array.prototype.cut back
!
const numbers = [1, 2, 3, 4]; const sum = numbers.cut back((a, b) => a + b, 0);
The 0
represents the beginning worth whereas with a
and b
, one represents the operating complete with the opposite representing the worth to be added. You may additionally observe that utilizing cut back
prevents unintended effects! I would nonetheless want one thing like Math.sum(...numbers)
however a easy cut back
will do!
Welcome to My New Workplace
My first skilled internet growth was at a small print store the place I sat in a windowless cubical all day. I suffered that boxed in atmosphere for nearly 5 years earlier than I used to be capable of finding a distant job the place I labored from house. The primary…
Serving Fonts from CDN
For max efficiency, everyone knows we should put our belongings on CDN (one other area). Together with these belongings are customized internet fonts. Sadly customized internet fonts by way of CDN (or any cross-domain font request) do not work in Firefox or Web Explorer (appropriately so, by spec) although…
Assign Anchor IDs Utilizing MooTools 1.2
Considered one of my favourite makes use of of the MooTools JavaScript library is the SmoothScroll plugin. I apply it to my web site, my employer’s web site, and on many buyer web sites. The perfect half concerning the plugin is that it is really easy to implement. I lately ran…
HTML5’s window.postMessage API
One of many little identified HTML5 APIs is the window.postMessage API.
window.postMessage
permits for sending information messages between two home windows/frames throughout domains. Basically window.postMessage acts as cross-domain AJAX with out the server shims. Let’s check out howwindow.postMessage
works and the way you…