{"id":5009,"date":"2023-04-19T22:52:00","date_gmt":"2023-04-20T03:52:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5009"},"modified":"2023-04-19T22:55:21","modified_gmt":"2023-04-20T03:55:21","slug":"simple-javascript-object-code-example","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/simple-javascript-object-code-example\/","title":{"rendered":"Simple JavaScript Object &#8211; Code Example"},"content":{"rendered":"\n<p>Below is a code example for creating a basic object and using a function to calculate the fuel economy.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"495\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9-1024x495.png\" alt=\"\" class=\"wp-image-5019\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9-1024x495.png 1024w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9-300x145.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9-768x372.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9-500x242.png 500w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-9.png 1106w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ New object Car\nconst car = {\n    make: 'Honda',\n    model: 'Civic',\n    topSpeed: 100,\n    tankCapacity: 10,\n    range: 300,\n    MPG: function() {\n        this.mpg = this.range \/ this.tankCapacity;\n        return this.mpg\n    }\n}\n\ncar.MPG();  \/\/ We need to call this to calculate the MPG, otherwise we get undefined\n\nconsole.log(`My car is a ${car.make + \" \" + car.model }, can go ${car.topSpeed}\/MPH, and gets ${car.mpg}\/MPG `)\n\n\/\/ Alternatively we can call the function car.MPG() directly.  \n\/\/ This keeps us from having to run the function before logging.\nconsole.log(`My car is a ${car.make + \" \" + car.model }, can go ${car.topSpeed}\/MPH, and gets ${car.MPG()}\/MPG `)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is a code example for creating a basic object and using a function to calculate the fuel economy.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1460,792],"tags":[644,1437,1463,1461,1462,606],"class_list":["post-5009","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","tag-code","tag-javascript","tag-js","tag-object","tag-oop","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5009"}],"version-history":[{"count":6,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5009\/revisions"}],"predecessor-version":[{"id":5020,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5009\/revisions\/5020"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}