Posts

Showing posts with the label es proposal

ES proposal: Object.getOwnPropertyDescriptors()

The ECMAScript proposal “ Object.getOwnPropertyDescriptors() ” by Jordan Harband and Andrea Giammarchi is part of ECMAScript 2017 . This blog post explains it. Overview Object.getOwnPropertyDescriptors(obj) accepts an object obj and returns an object result : For each own (non-inherited) property of obj , it adds a property to result whose key is the same and whose value is the the former property’s descriptor . Property descriptors describe the attributes of a property (its value, whether it is writable, etc.). For more information, consult Sect. “ Property Attributes and Property Descriptors ” in “Speaking JavaScript”. This is an example of using Object.getOwnPropertyDescriptors() : const obj = { [Symbol('foo')]: 123, get bar() { return 'abc' }, }; console.log(Object.getOwnPropertyDescriptors(obj)); // Output: // { [Symbol('foo')]: // { value: 123, // writable: true, // enumerable: true, ...

ECMAScript 2017: the final feature set

Check out my book (free online): “ Exploring ES2016 and ES2017 ”. Buy it now to get all future updates (“Shared memory and atomics” will be added soon). Update 2017-01-29: At the TC39 meeting in January 2017, the last feature of ECMAScript 2017, “ Shared memory and atomics ” advanced to stage 4. That means that its feature set is now complete, as listed below. The features of ECMAScript 2017 Major new features: Async Functions (Brian Terlson) Shared memory and atomics (Lars T. Hansen) Minor new features: Object.values/Object.entries (Jordan Harband) String padding (Jordan Harband, Rick Waldron) Object.getOwnPropertyDescriptors() (Jordan Harband, Andrea Giammarchi) Trailing commas in function parameter lists and calls (Jeff Morrison)

ES proposal: async functions

Async functions are an ECMAScript proposal by Brian Terlson. It is at stage 3 (candidate). Before I can explain async functions, I need to explain how Promises and generators can be combined to perform asynchronous operations via synchronous-looking code. Writing async code via Promises and generators For functions that compute their one-off results asynchronously, Promises, which are part of ES6, are becoming increasingly popular. One example is the client-side fetch API , which is an alternative to XMLHttpRequest for retrieving files. Using it looks as follows: function fetchJson(url) { return fetch(url) .then(request => request.text()) .then(text => { return JSON.parse(text); }) .catch(error => { console.log(`ERROR: ${error.stack}`); }); } fetchJson('http://example.com/some_file.json') .then(obj => console.log(obj)); co is a library that uses Promises and generators to enable...

ES proposal: Trailing commas in function parameter lists and calls

The following ECMAScript proposal is at stage 3 : “ Trailing commas in function parameter lists and calls ” by Jeff Morrison. This blog post explains it. Trailing commas in object literals and Array literals Trailing commas are ignored in object literals: let obj = { first: 'Jane', last: 'Doe', }; And they are also ignored in Array literals: let arr = [ 'red', 'green', 'blue', ]; console.log(arr.length); // 3 Why is that useful? There are two benefits. First, rearranging items is simpler, because you don’t have to add and remove commas if the last item changes its position. Second, it helps version control systems with tracking what actually changed. For example, going from: [ 'foo' ] to: [ 'foo', 'bar' ] leads to both the line with 'foo' and the line with 'bar' being marked as changed, even though the only...

ES proposal: string padding

The ECMAScript proposal “ String padding ” by Jordan Harband & Rick Waldron is part of ECMAScript 2017 . This blog post explains it. Padding strings Use cases for padding strings include: Displaying tabular data in a monospaced font. Adding a count or an ID to a file name or a URL: 'file 001.txt' Aligning console output: 'Test 001: ✓' Printing hexadecimal or binary numbers that have a fixed number of digits: '0x00FF' String.prototype.padStart(maxLength, fillString=' ') This method (possibly repeatedly) prefixes the receiver with fillString , until its length is maxLength : > 'x'.padStart(5, 'ab') 'ababx' If necessary, a fragment of fillString is used so that the result’s length is exactly maxLength : > 'x'.padStart(4, 'ab') 'abax' If the receiver is as long as, or longer than, maxLength , it is returned unchanged: > 'abcd'.padStart(2, '#') 'abcd'...

ES proposal: Object.entries() and Object.values()

The following ECMAScript proposal is at stage 4 : “ Object.values/Object.entries ” by Jordan Harband. This blog post explains it. Object.entries() This method has the following signature: Object.entries(value : any) : Array<[string,any]> If a JavaScript data structure has keys and values then an entry is a key-value pair, encoded as a 2-element Array. Object.entries(x) coerces x to an Object and returns the entries of its enumerable own string-keyed properties, in an Array: > Object.entries({ one: 1, two: 2 }) [ [ 'one', 1 ], [ 'two', 2 ] ] Properties, whose keys are symbols, are ignored: > Object.entries({ [Symbol()]: 123, foo: 'abc' }); [ [ 'foo', 'abc' ] ] Object.entries() finally gives us a way to iterate over the properties of an object ( read here why objects aren’t iterable by default ): let obj = { one: 1, two: 2 }; for (let [k,v] of Object.entries(obj)) { console.log(`${JSON.stringify(k...

The TC39 process for ECMAScript features

This blog post explains the so-called TC39 process , which governs how ECMAScript features are designed, starting with ECMAScript 2016 (ES7). Related blog posts: The final feature set of ECMAScript 2016 (ES7) Feature watch: ECMAScript 2017 Who designs ECMAScript? Answer: TC39 (Technical Committee 39). TC39 is the committee that evolves JavaScript. Its members are companies (among others, all major browser vendors). TC39 meets regularly , its meetings are attended by delegates that members send and by invited experts. Minutes of the meetings are available online and give you a good idea of how TC39 works. Occasionally (even in this blog post), you’ll see the term TC39 member referring to a human. Then it means: a delegate sent by a TC39 member company. It is interesting to note that TC39 operates by consensus: Decisions require that a large majority agrees and nobody disagrees strongly enough to veto. For many members, agreements lead to real obligations (they’ll have to implement fe...