{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/javascript-advance-concept/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"0dca6596-675c-5a40-88ab-6d8c26ea9ecc","excerpt":"In this article i will explain you some of javascript advance concept 1: Symbol Symbols are often used to add unique property keys to an object that won't…","html":"<p>In this article i will explain you some of javascript advance concept</p>\n<h4>1: Symbol</h4>\n<p>Symbols are often used to add unique property keys to an object that won't collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object.</p>\n<p>For Example:</p>\n<pre><code class=\"language-js\">const uid = Symbol(\"uid\");\nconst user = {\n  [uid]: \"p1\",\n  name: \"Max\",\n  age: 30,\n  [Symbol.toStringTag]: \"User\",\n};\n\n//Here we can only modify the user object only if we know the symbol id that is uid\nuser[uid] = \"p3\";\n\n//Also uid is hidden for outside , accessing the object\n//Below will return undefined\nconsole.log(user[Symbol(\"uid\")]);\n\n// Symbol.toStringTag helps in overriding toString() method of javascript we can give custom information\n\n//below will print [object User]\nconsole.log(user.toString());\n</code></pre>\n<h4>2: Iterators and Generators</h4>\n<h5>Iterators:</h5>\n<p>In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination.\nAn iterator is any object which implements the Iterator protocol by having a next() method that returns an object with two properties value and done</p>\n<p>For Example:</p>\n<pre><code class=\"language-js\">const company = {\n  curEmployee: 0,\n  employees: [\"Abc\", \"Def\", \"Ghi\"],\n  next() {\n    if (this.curEmployee >= this.employees.length) {\n      return { value: this.curEmployee, done: true };\n    }\n    const returnValue = {\n      value: this.employees[this.currentEmployee],\n      done: false,\n    };\n    this.curEmployee++;\n    return returnValue;\n  },\n};\n\nconsole.log(company.next());\nconsole.log(company.next());\n</code></pre>\n<h5>Generator:</h5>\n<p>Generator is a special type of javascript function which automatically generates an iterative object which has next method\nThe function* declaration (function keyword followed by an asterisk) defines a generator function.</p>\n<p>We use yield with generator function which saves the current state of exection and next time you call the generator function it resumes from where it left off.</p>\n<p>For example:</p>\n<pre><code class=\"language-js\">const company = {\n  curEmployee: 0,\n  employees: [\"Abc\", \"Def\", \"Ghi\"],\n  //Here * with the function indicate that it is a generator function\n  [Symbol.iterator]: function* employeeGenerator() {\n    let currentEmployee = 0;\n    while (currentEmployee &#x3C; this.employees.length) {\n      // Yield here save the current state of execution and next time you call the next() method it continues from\n      // Where it left off\n      yield this.employees[currentEmployee];\n      currentEmployee++;\n    }\n  },\n};\n\nfor (const employee of company) {\n  console.log(employee);\n}\n</code></pre>\n<h4>3: Reflect API</h4>\n<p>Reflect api in javascript helps to control objects. Reflect api gives us everything which helps in working with object\nlike object function which helps in performing action on any object</p>\n<p>For example:</p>\n<pre><code class=\"language-js\">const course = {\n  title: \"My Course\",\n};\n\n//Here we are controlling toString method in javascript\nReflect.setPrototypeOf(course, {\n  toString() {\n    return this.title;\n  },\n});\n\n//Reflect.deleteProperty(course, \"title\");\n\nconsole.log(course.toString());\n</code></pre>\n<h4>4: Proxy API</h4>\n<p>It creates traps for object operations for example if you want to return some info from object then you can create proxy api to return that information</p>\n<p>Example:</p>\n<pre><code class=\"language-js\">const course = {\n  title: \"My Course\",\n};\n\nconst courseHandler = {\n  get(obj, propertyName) {\n    console.log(propertyName);\n    return obj[propertyName];\n  },\n};\n\nconst pCourse = new Proxy(course, courseHandler);\n\nconsole.log(pCourse.title);\n</code></pre>","fields":{"slug":"/javascript-advance-concept/"},"frontmatter":{"title":"Javascript Advance Concept Symbols, Generators & Iterators, Reflect API, Proxy API","date":"October 30, 2022","description":"In this article i will explain you some of JavaScript advance concept like Symbol, Iterators, Generators, Reflect API, Proxy API","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Javascript.jpg"}},"previous":{"fields":{"slug":"/install-mysql-server-apple-m1-chip/"},"frontmatter":{"title":"Install and Connect MySQL and MySQL Workbench on Apple M1 chip","date":"October 20, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/MySql.jpg"}},"next":{"fields":{"slug":"/mongodb-creating-replicaset/"},"frontmatter":{"title":"MongoDB Replica Set - Step by Step With Example","date":"November 01, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mongo.jpg"}}},"pageContext":{"id":"0dca6596-675c-5a40-88ab-6d8c26ea9ecc","previousPostId":"b7c269f5-28ce-5131-b84d-3ff6adea9dac","nextPostId":"e7a83800-bec8-558f-b143-1faac990dc88"}},
    "staticQueryHashes": ["3860684146"]}