THE PRIMITIVE TYPE SYMBOL

JS

Symbol is a new JS data primitive introduced with the 2015 ECMA specification (ES6). A symbol represents a unique and anonymous value. To create them, we cannot use literal syntax but use the type Symbol. Symbols are very useful when we want to avoid property conflicts on an object. Another example of use is when we want to pass a unique, nonreplicable value to a function.

SAMPLE CODE

Copy to Clipboard

IMPLEMENT ITERATOR PROTOCOL

An iterator to be such must be able to access a sequence of elements one at a time. Having implemented the Iterator protocol does not mean that we can scroll through, for example, an array with a for of.

Copy to Clipboard
Iterator

IMPLEMENT THE ITERABLE PROTOCOL



An Iterable object allows us to customize how a sequence is traversed, such as through a for loop of.
The Iterable protocol must implement a property next whose value is a function.

Copy to Clipboard

ASYNC ITERATOR

We can implement the Iterable protocol with an asynchronous Iterator.

Copy to Clipboard

INTRODUCTION TO GENERATORS

Functions as we know are a block of code executed in full or at any rate, up to the return. With generators we have a partial mechanism for executing function code. We can start the function and perform the stop at a certain point. We run one portion of the function, then another portion, and so on, all with internal state maintenance.

Copy to Clipboard

IMPORT AND EXPORT VALUES FROM A GENERATOR (CODE)

Copy to Clipboard

CREATE AN INFINITE ITERATOR WITH GENERATORS

The generators implement the Iterable and Iterator protocols, since they implement Iterable we can use a for of to iterate over the elements.

Copy to Clipboard

LINKS TO PREVIOUS POSTS

THE JAVASCRIPT LANGUAGE

LINK TO CODE ON GITHUB

GITHUB