Symbols | ES6 JAGADEESH PATTA ( PJ )
Agenda  Introduction to Symbol  Symbol Syntax  Shared symbols in global registry.  Symbols in Object  Live examples
Introduction to Symbol  Symbol is a primitive data type.  Symbols are created via a factory function.  No two symbols are equal Because each symbol has its own unique id.  Symbol has optional string-valued parameter (Description).  We can convert a symbol to string by using toString() method.
Symbol Syntax Syntax let symbol_name = Symbol ( Description ); // Description is an optional parameter. Example let sym = Symbol(“one”);
Shared Symbols in global registry Using Symbol ( ) factory function will not create a global symbol that is available in your hole codebase. Create a symbol in registry Symbol.for(“description”). Get Symbol from registry Symbol.keyFor(“description”)
Symbols in object  We can use symbols as object keys.  By using Object.keys(obj) gives all keys of object as an array. But if the object uses symbols as keys this method will not work.  Object.getOwnPropertySymbols(obj) will gives you an iterator function. By using for..of we can get symbols one by one.
Symbols in object (cont…) Create Symbol In Object var obj = { Symbol(“name”) : “PJ”, Symbol(“name”) : “Jagadeesh Patta” }
Symbols in object (cont…) Get Symbols From Object var keys = Object.getOwnPropertySymbols( obj ); for ( let symbol of key){ console.log( obj[ symbol ] ); }
Any Q ?
Thank You

10. symbols | ES6 | JavaScript | TypeScript

  • 1.
  • 2.
    Agenda  Introduction toSymbol  Symbol Syntax  Shared symbols in global registry.  Symbols in Object  Live examples
  • 3.
    Introduction to Symbol Symbol is a primitive data type.  Symbols are created via a factory function.  No two symbols are equal Because each symbol has its own unique id.  Symbol has optional string-valued parameter (Description).  We can convert a symbol to string by using toString() method.
  • 4.
    Symbol Syntax Syntax let symbol_name= Symbol ( Description ); // Description is an optional parameter. Example let sym = Symbol(“one”);
  • 5.
    Shared Symbols inglobal registry Using Symbol ( ) factory function will not create a global symbol that is available in your hole codebase. Create a symbol in registry Symbol.for(“description”). Get Symbol from registry Symbol.keyFor(“description”)
  • 6.
    Symbols in object We can use symbols as object keys.  By using Object.keys(obj) gives all keys of object as an array. But if the object uses symbols as keys this method will not work.  Object.getOwnPropertySymbols(obj) will gives you an iterator function. By using for..of we can get symbols one by one.
  • 7.
    Symbols in object(cont…) Create Symbol In Object var obj = { Symbol(“name”) : “PJ”, Symbol(“name”) : “Jagadeesh Patta” }
  • 8.
    Symbols in object(cont…) Get Symbols From Object var keys = Object.getOwnPropertySymbols( obj ); for ( let symbol of key){ console.log( obj[ symbol ] ); }
  • 9.
  • 10.