Sharing Top Content from the Angular-sphere.

JavaScript Symbols, Generators and Streams – Frontend Weekly – Medium

  • Enjoy it!What is Symbol?There’s some curious specifications about it:They’re primitive data type;They’re factory functions, not constructors (you can’t use the new keyword);They are unique: Symbol(“foo”) == Symbol(“foo”) returns false;Can be used as non-enumerable object property keys.The two last parts are pretty important, so let’s go deeper on it.Staying anonymousSo if you…
  • For iteration over object we have a better solution (and we’re still going to use Symbols for that):Well-known SymbolsThere’s a special type of Symbol, which we can use to create a default iterator method for our objects, just using it as the property key.Note from the writer:This really reminds me…
  • To declare this type of functions we need to use the following notation:function* myGenerator() { … }This way, when invoked, this function will return a generator object, which is a iterable too (have the next() method returning the {value, done} object).
  • To simplify the above explanation, in the example below, we see that we’re able to use the iterator method as a generator function:For each call of the function, it will execute until find the next yield, returning the value and freezing the context.
  • Let’s create a factory function capable of doing this job:The payment train have now two wagons of gold (waiting to be filled) and one of people (don’t need the workers to be filled).

The Symbol and Generator were added on ES2015 and they use is pretty blurry for some developers. So, in this article, I’ll try to simplify and explain it, then show some use cases. My objective isn’t…

IntroductionThe Symbol and Generator were added on ES2015 and they use is pretty blurry for some developers. So, in this article, I’ll try to simplify and explain it, then show some use cases.My objective isn’t to show everything related to them, but touch in some interesting aspects of both and the correlation between they usages, trying to being simple and concise for better understanding. Enjoy it!What is Symbol?There’s some curious specifications about it:They’re primitive data type;They’re factory functions, not constructors (you can’t use the new keyword);They are unique: Symbol(“foo”) == Symbol(“foo”) returns false;Can be used as non-enumerable object property keys.The two last parts are pretty important, so let’s go deeper on it.Staying anonymousSo if you want to protect some properties/methods of an Object, you can basically do this using the Symbol instances in object literals:And in classes/constructors you can do like this:Note: As JS objects aren’t restricted to “strings” as property keys, we can use the Computed property names to use a Symbol instance as a property of literal and constructed object instances.Pretty easy right? And when you try to enumerate the properties of this instance (like using Object.keys), you’re going to have only:[ ‘hello’ ]And you may think, “this is great for creating object where I can iterate over then!”. Actually the Symbols are more for accessing protection. For iteration over object we have a better solution (and we’re still going to use Symbols for that):Well-known SymbolsThere’s a special type of Symbol, which we can use to create a default…

JavaScript Symbols, Generators and Streams – Frontend Weekly – Medium

Comments are closed, but trackbacks and pingbacks are open.