ES8

ES8

ECMAScript 8 or ECMAScript 2017 was officially  released at the end of June by TC39. There are bunch of new features were introduced.

  • Object.values()
  • Object.entries()
  • String.prototype.padStart()
  • String.prototype.padEnd()
  • Object.getOwnPropertyDescriptors()
  • Async Functions
  • Trailing commas in Function

  • Object.values() returns an array of a given object’s own enumerable property values
const obj = {
  name: 'Anne',
  age: 20,
  gender: 'male'
};

Object.values(obj); //["Anne", 20, "male"]

  • Object.entries() returns an array of a given object’s own enumerable string-keyed property [key, value] method pads the current string with another string
const obj = {
  name: 'Anne',
  age: 20,
  gender: 'male'
};

Object.entries(obj); // [["name", "Anne"], ["age", 20], ["gender", "male"]]

  • String.prototype.padStart() method pads the current string with a given string .
const name = ' Anne  ';

name.padStart(); //'Anne'

  • String.prototype.padEnd() method pads the current string with a given string. The padding is applied from the end of the current string.
const name = 'Anne  ';

name.padEnd(); //'Anne'

  • Object.getOwnPropertyDescriptors() method returns all own property descriptors of a given object
const obj = {
  name: 'Anne',
  age: 20,
  gender: 'male'
};

Object.getOwnPropertyDescriptors(obj);

//{
// age: {value: 20, writable: true, enumerable: true, configurable: true}
// gender: {value: "male", writable: true, enumerable: true, configurable: true}
// name: {value: "Anne", writable: true, enumerable: true, configurable: true}
//}

  • Async Functions
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
};

var add = async function(x) {
  var a = await resolveAfter2Seconds(20);
  var b = await resolveAfter2Seconds(30);
  return x + a + b;
};

  • Trailing commas in functions.
    If you want to add a new property, you can simply add a new line without modifying the previously last line if that line already uses a trailing comma. This makes version-control diffs cleaner and editing code might be less troublesome.

    JavaScript has allowed trailing commas in array literals since the beginning, and later added them to object literals (ECMAScript 5) and most recently (ECMAScript 2017) to function parameters.

Following functions, One and Two are same.

function one(p) {}
function two(p,) {}

class C {
  one(a,) {},
  two(a, b,) {},
}

var obj = {
  one(a,) {},
  two(a, b,) {},
};

Summaries

There are several great javascript features were released under ECMAScript 8 or ECMAScript 2017. However, it will take some time to reflect these features in modern bowsers.

Leave a Reply