{"componentChunkName":"component---src-templates-post-js","path":"/blog/2016/07/06/es6-new-features-part-i/","result":{"data":{"post":{"html":"<p>Well for a long time I wanted to write a blog post about migrating Angular 1.x to 2, however, before doing so I thought it is really helpful to write about some prerequisites of that.</p>\n<!--more-->\n<p>This is the first article in ES6 new features series.</p>\n<p><a href=\"/2016/07/07/es6-new-features-part-ii/\">Part II</a></p>\n<p><a href=\"/2016/07/11/es6-new-features-part-iii/\">Part III</a></p>\n<p>Number one in the list would be ES6. Which is the topic of today’s post. ES6 (often referred to as “Harmony”) is the upcoming sixth major release of the ECMAScript language specification.</p>\n<p>However, that does not put things in perspective. I prefer to say it is the newest JavaScript implementation, simple as that.</p>\n<p>I just briefly list them here and go through one by one:</p>\n<ol>\n<li>Default Parameters in ES6</li>\n<li>Template Literals in ES6</li>\n<li>Multi-line Strings in ES6</li>\n<li>Destructuring Assignment in ES6</li>\n<li>Enhanced Object Literals in ES6</li>\n<li>Arrow Functions in ES6</li>\n<li>Promises in ES6</li>\n<li>Block-Scoped Constructs Let and Const</li>\n<li>Classes in ES6</li>\n<li>Modules in ES6\r\nAlthough it might look like a short list but they are the most important ones.</li>\n</ol>\n<h3 id=\"1-default-parameters-in-es6\" style=\"position:relative;\"><a href=\"#1-default-parameters-in-es6\" aria-label=\"1 default parameters in es6 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Default Parameters in ES6</h3>\n<p>If you remember previously we had to explicitly specify a value if the coming parameter to a method didn’t have one.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6896370952037944000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var bar = function(foo) {\r\n  var temp = foo || 'default value'\r\n}`, `6896370952037944000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">bar</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">foo</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">var</span> temp <span class=\"token operator\">=</span> foo <span class=\"token operator\">||</span> <span class=\"token string\">'default value'</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>Now it is like one of high level languages such as C#.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"12313107728909834000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var bar = function(foo = 'default value') {\r\n  //code goes here\r\n}`, `12313107728909834000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">bar</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span>foo <span class=\"token operator\">=</span> <span class=\"token string\">'default value'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">//code goes here</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"2-template-literals-in-es6\" style=\"position:relative;\"><a href=\"#2-template-literals-in-es6\" aria-label=\"2 template literals in es6 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Template Literals in ES6</h3>\n<p>Template literals or interpolation in other languages is a way to output variables in the string. So in ES5 we had to break the string like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"30516627442097066000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var fullname = firstname + ' ' + lastname`, `30516627442097066000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> fullname <span class=\"token operator\">=</span> firstname <span class=\"token operator\">+</span> <span class=\"token string\">' '</span> <span class=\"token operator\">+</span> lastname</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>In ES6 though you can use the new syntax ${} which is again like C#:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"58979210602104690000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var fullname = \\`Your name is \\${firstname} \\${lastname}.\\``, `58979210602104690000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> fullname <span class=\"token operator\">=</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Your name is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>firstname<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>lastname<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">.</span><span class=\"token template-punctuation string\">`</span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"3-multi-line-strings-in-es6\" style=\"position:relative;\"><a href=\"#3-multi-line-strings-in-es6\" aria-label=\"3 multi line strings in es6 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Multi-line Strings in ES6</h3>\n<p>Previously we had to use + to shape multiple line strings something like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"31525785119296954000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var fourAgreements =\r\n  'You have the right to be you.' + 'You can only be you when you do your best.'`, `31525785119296954000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> fourAgreements <span class=\"token operator\">=</span>\r\n  <span class=\"token string\">'You have the right to be you.'</span> <span class=\"token operator\">+</span> <span class=\"token string\">'You can only be you when you do your best.'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>But in ES6 they introduced the multi-line string literal ` which makes life easier:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"59228531360032145000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var fourAgreements = \\`You have the right to be you.\r\n    You can only be you when you do your best.\\``, `59228531360032145000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> fourAgreements <span class=\"token operator\">=</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">You have the right to be you.\r\n    You can only be you when you do your best.</span><span class=\"token template-punctuation string\">`</span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"4-destructuring-assignment-in-es6\" style=\"position:relative;\"><a href=\"#4-destructuring-assignment-in-es6\" aria-label=\"4 destructuring assignment in es6 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Destructuring Assignment in ES6</h3>\n<p>This can be a little confusing as there is some logic behind it. In ES5 when we wanted to extract  some properties from an object we should define them as below:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"87365282732101090000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var data = \\$('body').data(), // data has properties cat and mouse\r\n  house = data.cat,\r\n  mouse = data.mouse`, `87365282732101090000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> data <span class=\"token operator\">=</span> <span class=\"token function\">$</span><span class=\"token punctuation\">(</span><span class=\"token string\">'body'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">data</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// data has properties cat and mouse</span>\r\n  house <span class=\"token operator\">=</span> data<span class=\"token punctuation\">.</span>cat<span class=\"token punctuation\">,</span>\r\n  mouse <span class=\"token operator\">=</span> data<span class=\"token punctuation\">.</span>mouse</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>In ES6, we can replace the ES5 code above with these statements:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"22936087227017940000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var { cat, mouse } = \\$('body').data() // we'll get house and mouse variables`, `22936087227017940000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> <span class=\"token punctuation\">{</span> cat<span class=\"token punctuation\">,</span> mouse <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> <span class=\"token function\">$</span><span class=\"token punctuation\">(</span><span class=\"token string\">'body'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">data</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// we'll get house and mouse variables</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"5-enhanced-object-literals-in-es6\" style=\"position:relative;\"><a href=\"#5-enhanced-object-literals-in-es6\" aria-label=\"5 enhanced object literals in es6 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. Enhanced Object Literals in ES6</h3>\n<p>First of all let’s see what is an object literal. An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces (<code class=\"language-text\">{}</code>).</p>\n<p>What you can do with object literals now in ES6 is mind blowing! It has started from something like a glorified JSON in ES5 to something like a class in ES6.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91537893529839400000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var sales = 'Toyota'\r\n\r\nfunction carTypes(name) {\r\n  if (name === 'Honda') {\r\n    return name\r\n  } else {\r\n    return &quot;Sorry, we don't sell &quot; + name + '.'\r\n  }\r\n}\r\n\r\nvar car = { myCar: 'Saturn', getCar: carTypes('Honda'), special: sales }\r\n\r\nconsole.log(car.myCar) // Saturn\r\nconsole.log(car.getCar) // Honda\r\nconsole.log(car.special) // Toyota`, `91537893529839400000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> sales <span class=\"token operator\">=</span> <span class=\"token string\">'Toyota'</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">carTypes</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">name</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>name <span class=\"token operator\">===</span> <span class=\"token string\">'Honda'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> name\r\n  <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> <span class=\"token string\">\"Sorry, we don't sell \"</span> <span class=\"token operator\">+</span> name <span class=\"token operator\">+</span> <span class=\"token string\">'.'</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">var</span> car <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">myCar</span><span class=\"token operator\">:</span> <span class=\"token string\">'Saturn'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">getCar</span><span class=\"token operator\">:</span> <span class=\"token function\">carTypes</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Honda'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">special</span><span class=\"token operator\">:</span> sales <span class=\"token punctuation\">}</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>car<span class=\"token punctuation\">.</span>myCar<span class=\"token punctuation\">)</span> <span class=\"token comment\">// Saturn</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>car<span class=\"token punctuation\">.</span>getCar<span class=\"token punctuation\">)</span> <span class=\"token comment\">// Honda</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>car<span class=\"token punctuation\">.</span>special<span class=\"token punctuation\">)</span> <span class=\"token comment\">// Toyota</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"83488440763124120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var car = { manyCars: { a: 'Saab', b: 'Jeep' }, 7: 'Mazda' }\r\n\r\nconsole.log(car.manyCars.b) // Jeep\r\nconsole.log(car[7]) // Mazda`, `83488440763124120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> car <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">manyCars</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">a</span><span class=\"token operator\">:</span> <span class=\"token string\">'Saab'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">b</span><span class=\"token operator\">:</span> <span class=\"token string\">'Jeep'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token operator\">:</span> <span class=\"token string\">'Mazda'</span> <span class=\"token punctuation\">}</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>car<span class=\"token punctuation\">.</span>manyCars<span class=\"token punctuation\">.</span>b<span class=\"token punctuation\">)</span> <span class=\"token comment\">// Jeep</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>car<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// Mazda</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In ES6 Object Literals are extended to support setting the prototype at construction, shorthand for <code class=\"language-text\">foo: foo</code> assignments, defining methods, making super calls, and computing property names with expressions.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"56085773579430540000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var obj = {\r\n  // __proto__\r\n  __proto__: theProtoObj,\r\n  // Shorthand for ‘handler: handler’\r\n  handler,\r\n  // Methods\r\n  toString() {\r\n    // Super calls\r\n    return 'd ' + super.toString()\r\n  },\r\n  // Computed (dynamic) property names\r\n  ['prop_' + (() => 42)()]: 42,\r\n}`, `56085773579430540000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> obj <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// __proto__</span>\r\n  <span class=\"token literal-property property\">__proto__</span><span class=\"token operator\">:</span> theProtoObj<span class=\"token punctuation\">,</span>\r\n  <span class=\"token comment\">// Shorthand for ‘handler: handler’</span>\r\n  handler<span class=\"token punctuation\">,</span>\r\n  <span class=\"token comment\">// Methods</span>\r\n  <span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// Super calls</span>\r\n    <span class=\"token keyword\">return</span> <span class=\"token string\">'d '</span> <span class=\"token operator\">+</span> <span class=\"token keyword\">super</span><span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token comment\">// Computed (dynamic) property names</span>\r\n  <span class=\"token punctuation\">[</span><span class=\"token string\">'prop_'</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token number\">42</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span><span class=\"token operator\">:</span> <span class=\"token number\">42</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>","timeToRead":3,"frontmatter":{"unformattedDate":"2016-07-06T00:00:00.000Z","date":"July 06, 2016","title":"ES6 new features (Part I)","subtitle":null,"id":7,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["ES6","features"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEUUlEQVR42n1UC1BUZRTeQFiB5bkQyEOX177kIUKurgoFZIaBGUUSUDwCQaRhMkUbciwQRkeDkKlhBUZDHiFmk8NMOmklTvIm2aJ4qcW6GOpkubjt7r3367/LYy4++me++e+955zv/8655z883uwC8BSBJeedTyAmiCR4kSCWIIxA+KQY7mINFrNOERrtnypVffN4QWGxKT1zO1JSM5D2Vg6y83YxJeVVd76/1HWB+GUT2M/GWPC4p8zuiwgOHamsoRQKBTYoBczuNCvUvG+HhhIBc7LUninL5+ONjXysCvNhkpLfRsflzhES8/xs/EKlev305+lZ+VDIeFRfo6sJI1IKvwczzPVgYDIUt36UwjASBNxYTt+/IjIdKrCkRN6OOFbXQLiQsUApeUgp2vsRNil4JowFMRiWg+qTQN9DSjgkxanDnkh7yQEYlMDYKwF9VQJMhGD4rBfl62mJppYvdYRjxby6Hy53nVdGLMPdbn8Kahn+JUT0AAkmpBiVoSDZGWUFrsCwDCbyzdRPDusKJGpD0NHgZlqvVGBCo62dU2d3uEI1nhpPFIzLabOCgRmAKNF3i9HdtAwMUcr8JDFjzs76YlhCJ8S4ov5E28AcodOBg9UTmVvsgDFC2Dfr3Cc2k013iWHsF+MB+9w5827oFZuJWbUYkdGpCY74uLJ2lHDZmAupqmtWJ24QAr9JaNaJURPHX6QAq0gtBaWeqSf9M/kxrJ3s7IEUSR2/iunNrMLjbf3zNezsudoSpQzEre+WUri2HMdLliBUwse6MBvUFLtj++tOkPtZo7Hc00yWsskBo2f9iAA5Ji/6UFFKCXr61Ce5vbgt8dVkNJXbU7gTiv25rhB5WaFitzvaq73hIVyErJcdceO8P2o+9IDQ0RI3LwaAtBUaygTUa0mpbOtkcQmXVn964m581GLgdjBTmk8Il1ih9B03/EPSOveZDyLDbZES54DnnrHFlmgB4tbZsT3JxK0nWRxrnCIcXjxuM96797cqKjIK6jPupoqiGRV5JNX9ea54JcYeyhU2iF5li41r7bA62AZpm51xoU5oio19AQaDoYrlaG1tteRx7rC8tr75fqzCGte+8WdqPvBAUYYL2o96o+2IF/ZmuUDzrT8GvhCh4r2nMdkRyKwOscWp0+23SayIe43n7yHZS5K2ZmJfrpUR0yuxLckJO7Y6oThHiIOFbshJdMQBskMfjswEnjEvv4itXeEjd5kzIPharfZSUHA46vYJTLpuOdRtIgyd8YX6tC9GvvaDrlOGPW9amJ6NiYdB/6BtgTLu4qQe0NPbf90/IBCqPdZG3AyiaW2oeUBgTEq9m8QzrlkbDY1Gc4X4Oj+RcMbQOpe6eHBwaCgmNgHRERb0JzttUJrHx0rZYjotfQempqbOER+XR2bh40nnlQp1Ot3Ryqrav9KzCpCduxONLV/9Qb7vIrD+X2WPI+UQkzGDNewkJxA8bH94/QeYyzHWiE3wRQAAAABJRU5ErkJggg=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/843eb0f0e2f739a8cf7490c23e695ace/be182/es6.png","srcSet":"/static/843eb0f0e2f739a8cf7490c23e695ace/be182/es6.png 150w","sizes":"(min-width: 150px) 150px, 100vw"},"sources":[{"srcSet":"/static/843eb0f0e2f739a8cf7490c23e695ace/5d458/es6.webp 150w","type":"image/webp","sizes":"(min-width: 150px) 150px, 100vw"}]},"width":150,"height":150}}},"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB8ElEQVR42h2PW4+bMBCF+c1V/0TfK+1L3/tUqX3YSlW16UbdxizBSbrJLhBiLgGHuw3Y3EyIaip9OjqjM6OZUV58YsTciJkZM5TxgPYzpDuTzit6v+hx2cf1cKkGTPsLbQPSenmDZZpWCrxUNhltIiSIjiamZlickvoYlVZUmjJNmB1RN60xbbP2FvHpwqa8vYWkU1Zhucs6I++tYjjRcbk9LtaHR2gsN9aj/rbQDqsX+0HbLzfG5hRiNoW1CKox5pOftwoISz3tYNK9pp2d9X41BWzC/Bawm1Tc3M5sCmTJJ6caHSr8cnRKcSSjETcKwCVMWt2vdLfcBew1auy8c+cmgesxqK4OnWccMkgQEW45P2gW4hAxBaAMOnRtE90roV9Dn238eh8weQUqhMTOhVUIiwqTzDqbfLCI2EdcAWaydenOKdaIgmO6stLVMVt7bO0zfYbrZ67jRk/7DRFaJJ5Owybu3wo5zBQVUe0P2P38plnJd2Dcg7cfEEHc6memB1wP+awBX5/ZLm8/f72+e3/9hfotHbSwVnSvun8Adx/vnlGuoeLZLTS/hLiBAYfhf6SRm3HzN2m+LPoPn8ZV0G/zAbhUeTqEKytbGql6yoGdAZQDj6jHRDUj1YpVQ2oCnAK4RKJ5GQwL1SMAkd9G/A8udhIOKMiB9gAAAABJRU5ErkJggg=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/a50db1f34e0fcbba398b0eccf9ffce9d/a2195/socialpreview.png","srcSet":"/static/a50db1f34e0fcbba398b0eccf9ffce9d/cadb4/socialpreview.png 750w,\n/static/a50db1f34e0fcbba398b0eccf9ffce9d/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/a50db1f34e0fcbba398b0eccf9ffce9d/11ba1/socialpreview.webp 750w,\n/static/a50db1f34e0fcbba398b0eccf9ffce9d/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":{"publicURL":"/static/843eb0f0e2f739a8cf7490c23e695ace/es6.png"},"socialPreview":{"publicURL":"/static/a50db1f34e0fcbba398b0eccf9ffce9d/socialpreview.png"}}}},"pageContext":{"title":"ES6 new features (Part I)","postPath":"/blog/2016/07/06/es6-new-features-part-i","nextPost":"/blog/2016/07/07/es6-new-features-part-ii","previousPost":"/blog/2016/07/02/angular-dependecy-injection-and-possible-issues-around-it"}},"staticQueryHashes":[],"slicesMap":{}}