{"id":12728,"date":"2020-04-11T03:37:50","date_gmt":"2020-04-11T03:37:50","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=12728"},"modified":"2020-05-27T08:03:12","modified_gmt":"2020-05-27T08:03:12","slug":"objects-in-java-script-explained","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/objects-in-java-script-explained\/","title":{"rendered":"Objects in java-script explained!"},"content":{"rendered":"\n<p><strong>Objects <\/strong>are used to store keyed collections of various data and more complex entities. In <strong>JavaScript<\/strong>, <strong>objects <\/strong>penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else.<\/p>\n\n\n\n<p>An <strong>object <\/strong>can be created with figure brackets&nbsp;<code>{\u2026}<\/code>&nbsp;with an optional list of&nbsp;<em>properties<\/em>. A property is a \u201ckey: value\u201d pair, where&nbsp;<code>key<\/code>&nbsp;is a string (also called a \u201cproperty name\u201d), and&nbsp;<code>value<\/code>&nbsp;can be anything.<\/p>\n\n\n\n<p>We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It\u2019s easy to find a file by its name or add\/remove a file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"673\" height=\"244\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/Object-in-javaScript-1.png\" alt=\"\" class=\"wp-image-12730\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/Object-in-javaScript-1.png 673w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/Object-in-javaScript-1-300x109.png 300w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><figcaption>An empty object (\u201cempty cabinet\u201d) can be created using one of two syntaxes:<\/figcaption><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Let&#8217;s understand it more simple:&#8211;<\/strong><\/p>\n\n\n\n<p>In <strong>JavaScript<\/strong>, almost &#8220;everything&#8221; is an object.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Booleans can be <strong>objects <\/strong>(if defined with the&nbsp;<code>new<\/code>&nbsp;keyword)<\/li><li>Numbers can be <strong>objects <\/strong>(if defined with the&nbsp;<code>new<\/code>&nbsp;keyword)<\/li><li>Strings can be <strong>objects <\/strong>(if defined with the&nbsp;<code>new<\/code>&nbsp;keyword)<\/li><li>Dates are always <strong>objects<\/strong><\/li><li>Maths are always <strong>objects<\/strong><\/li><li>Regular expressions are always <strong>objects<\/strong><\/li><li>Arrays are always <strong>objects<\/strong><\/li><li>Functions are always <strong>objects<\/strong><\/li><li>Objects are always <strong>objects<\/strong><\/li><\/ul>\n\n\n\n<p>All JavaScript values, except primitives, are <strong>objects<\/strong>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> user = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Object<\/span>(); <span class=\"hljs-comment\">\/\/ \"object constructor\" syntax<\/span>\n<span class=\"hljs-keyword\">let<\/span> user = {};  <span class=\"hljs-comment\">\/\/ \"object literal\" syntax<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"922\" height=\"592\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/object-code.png\" alt=\"\" class=\"wp-image-12731\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/object-code.png 922w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/object-code-300x193.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/object-code-768x493.png 768w\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><figcaption>object code example<\/figcaption><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Creating objects in JavaScript (3 Different Ways)<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Using functions as class:<\/strong><\/p>\n\n\n\n<p>One of the easiest way to instantiate an object in <strong>JavaScript<\/strong>. We define a classical <strong>JavaScript <\/strong>function and create an <strong>object <\/strong>of the <strong>function <\/strong>using&nbsp;<em>new<\/em>&nbsp;<strong>keyword<\/strong>. The <strong>properties <\/strong>and <strong>methods <\/strong>of <strong>function <\/strong>are created using the&nbsp;<em>this<\/em>&nbsp;<strong>keyword<\/strong>.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/e082fbcfd3bafc5bf450a66b60371b99.js\"><\/script>\n\n\n\n<p>OUT PUT :&#8211;  Vineet 20<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Using object literals:<\/strong><\/p>\n\n\n\n<p>Literals are smaller and simpler ways to define <strong>objects<\/strong>. Below we instantiate an <strong>object <\/strong>exactly same as the previous one just with the <strong>object <\/strong>literal.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/e082fbcfd3bafc5bf450a66b60371b99.js\"><\/script>\n\n\n\n<p>OUT PUT :&#8211; Vineet 20<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Singleton using a function:<\/strong><\/p>\n\n\n\n<p>The third way presented is a combination of the other two that we already saw. We can use a <strong>function <\/strong>to define a singleton <strong>object<\/strong>.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/e082fbcfd3bafc5bf450a66b60371b99.js\"><\/script>\n\n\n\n<p>OUT PUT:&#8211; Vineet 20<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Access Object Properties in JavaScript(3 Different Ways)<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Using Dot property accessor:&nbsp;<code>object.property<\/code><\/strong><\/p>\n\n\n\n<p>A common way to access the property of an <strong>object <\/strong>is the&nbsp;<em>dot property accessor<\/em>&nbsp;syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">expression<\/span><span class=\"hljs-selector-class\">.identifier<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>expression<\/code>&nbsp;should evaluate to an <strong>object<\/strong>, and&nbsp;<code>identifier<\/code>&nbsp;is the name of the <strong>property <\/strong>you\u2019d like to access.<\/p>\n\n\n\n<p>For example, let\u2019s access the <strong>property&nbsp;<\/strong><code>name<\/code>&nbsp;of the object&nbsp;<code>hero<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> hero = {\n  <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'Batman'<\/span>\n};\n\n<span class=\"hljs-comment\">\/\/ Dot property accessor<\/span>\nhero.name; <span class=\"hljs-comment\">\/\/ =&gt; 'Batman'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>hero.name<\/code>&nbsp;is a dot <strong>property <\/strong>accessor that reads the property&nbsp;<code>name<\/code>&nbsp;of the <strong>object&nbsp;<\/strong><code>hero<\/code>.<\/p>\n\n\n\n<p>You can use the dot property accessor in a chain to access deeper properties:&nbsp;<code>object.prop1.prop2<\/code>.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Square brackets property access:&nbsp;<code>object['property']<\/code><\/strong><\/p>\n\n\n\n<p>The square brackets property accessor has the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">expression<\/span><span class=\"hljs-selector-attr\">&#91;expression]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The first&nbsp;<code>expression<\/code>&nbsp;should evaluate to an object and the second&nbsp;<code>expression<\/code>&nbsp;should evaluate to a string denoting the property name.<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> property = <span class=\"hljs-string\">'name'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> hero = {\n  <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'Batman'<\/span>\n};\n\n<span class=\"hljs-comment\">\/\/ Square brackets property accessor:<\/span>\nhero&#91;<span class=\"hljs-string\">'name'<\/span>];   <span class=\"hljs-comment\">\/\/ =&gt; 'Batman'hero&#91;property]; \/\/ =&gt; 'Batman'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>hero['name']<\/code>&nbsp;and&nbsp;<code>hero[property]<\/code>&nbsp;both read the property&nbsp;<code>name<\/code>&nbsp;by using the square brackets syntax.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Using<\/strong> <strong>Object destructing:&nbsp;<code>const { property } = object<\/code><\/strong><\/p>\n\n\n\n<p>The basic object destructuring syntax is pretty simple:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> { identifier } = expression;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>identifier<\/code>&nbsp;is the name of the property to access and&nbsp;<code>expression<\/code>&nbsp;should evaluate to an object. After the destructuring, the variable&nbsp;<code>identifier<\/code>&nbsp;contains the property value.<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> hero = {\n  <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'Batman'<\/span>\n};\n\n<span class=\"hljs-comment\">\/\/ Object destructuring:<\/span>\n<span class=\"hljs-keyword\">const<\/span> { name } = hero;\nname; <span class=\"hljs-comment\">\/\/ =&gt; 'Batman'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>const { name } = hero<\/code>&nbsp;is an object destructuring. The destructuring defines a variable&nbsp;<code>name<\/code>&nbsp;with the value of property&nbsp;<code>name<\/code>.<\/p>\n\n\n\n<p>When you get used to object destructuring, you will find that its syntax is a great way to extract the properties into variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first&#8230; <\/p>\n","protected":false},"author":14,"featured_media":12734,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5691],"tags":[5431,5611,5295,4240,5435,3902,1073],"class_list":["post-12728","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-function","tag-javascript","tag-keyword","tag-method","tag-objects","tag-programming","tag-properties"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=12728"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12728\/revisions"}],"predecessor-version":[{"id":12736,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12728\/revisions\/12736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/12734"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=12728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=12728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=12728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}