New features of Javascript/ECMAScript I am starting to use

published Jun 11, 2016 12:50   by admin ( last modified Jun 11, 2016 09:02 )

Well, they may not in all cases be new to the world but here goes:

 

for ... of

Coming from python this is a no-brainer. Proper iteration over an array without getting properties

Object.keys() - JavaScript | MDN

Again, coming from python it is nice to go through the keys of an object (dictionary-style)

Arrow functions

Saves on keystrokes, and makes code more concise

Templates

It took a while to get used to the grave accents, but the fact that you can have multi line strings and have placeholders make this one interesting. Again, similar to python.

Maps

And instead of relying on Object.keys(), why not use a proper map/dictionary?

And here are things I will start to use I think:

Learning ES6: Enhanced object literals | Ben Ilegbod

From that page:

function getCar(make, model, value) {
	return {
		// with property value shorthand
		// syntax, you can omit the property
		// value if key matches variable
		// name
		make,  // same as make: make
		model, // same as model: model
		value, // same as value: value

		// computed values now work with
		// object literals
		['make' + make]: true,

		// Method definition shorthand syntax
		// omits `function` keyword & colon
		depreciate() {
			this.value -= 2500;
		}
	};
}

Ok, it turned out I needed all of those object literal improvements today, so it is now in usage by me. I needed an update to run on the nedb datbase and set a property which name is not known until runtime, so the weird [] construct was used (is Javascript turning into Perl? Using all letters?).  Before that I solved that conundrum by constructing JSON with string concatenation and then parse the result which was awkward. The shorthand for key-values worked fine in module.exports, and shorter formats of functions worked fine too.

Things I am not sure how I would benefit from using in my current style:

Symbol - JavaScript | MDN - Invisible properties, kind of.

Typed Arrays in ECMAScript 6 - Good if you work with binary data, e.g. interfacing with libraries in other languages.

Weak Maps - A way to associate data to objects which data go away (gets garbage collected) with the object.

Things I may stay away from:

Does HTML comment <!-- act as a single-line comment in JavaScript, and why? - Stack Overflow

Actually this is not new, more of a placeholder in ECMAScript documenting current browser behaviour.

Things that disappeared

Array comprehensions - were introduced and then removed(!)