A JavaScript link mashup for Winter 2017: Things I used and liked in the JS universe this year

2017 was a good year for JavaScript. The newer language features are now part of our daily routine. Tightly integrated CI and packaging scripts are the norm, and we have a huge sea of libraries and tools available to us. The core JavaScript frameworks have settled down and matured to the point that we can be confident in JS as the base for large-scale commercial projects. Here are a few of the main JavaScript things that impressed me this year: ...

December 11, 2017

Simple sets in JavaScript

One of my favourite JavaScript features is its literal object notation. It allows you to declare data structures in JSON, which is a very succinct, C-like syntax. You can use this notation to declare SETs, and the a in b syntax to verify a variable is in a set: /* Declare your set as a JSON constant */ var valid_widgets = { 'widget-viewer': 1, 'special-viewer': 1, 'widget-editor': 1 }; /* Fail if something isn't in the set */ if ( ! (options.widget in valid_widgets) ) return false; /* Profit! */ As the in keyword operates on the index of the referred object, we set the value of the constants to 1 (true). A bit of a hack, but it results in a very clean way of checking if something is part of a set of valid values. ...

May 29, 2012