{"id":694,"date":"2017-02-14T10:51:34","date_gmt":"2017-02-14T18:51:34","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=694"},"modified":"2017-02-14T10:51:34","modified_gmt":"2017-02-14T18:51:34","slug":"way-to-create-an-object-in-javascript","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=694","title":{"rendered":"Way to create an object in Javascript"},"content":{"rendered":"<p>   1) \/\/ object constructor <\/p>\n<pre>\r\nvar mango =  new Object ();\r\nmango.color = \"yellow\";\r\nmango.shape= \"round\";\r\nmango.sweetness = 8;\r\n\r\nmango.howSweetAmI = function () {\r\nconsole.log(\"Hmm Hmm Good\");\r\n}\r\n      <\/pre>\n<p>   2) \/\/ object literal<\/p>\n<pre>\r\n      \/\/ This is an object with 4 items, again using object literal\r\nvar mango = {\r\ncolor: \"yellow\",\r\nshape: \"round\",\r\nsweetness: 8,\r\n\r\nhowSweetAmI: function () {\r\nconsole.log(\"Hmm Hmm Good\");\r\n}\r\n}\r\n   <\/pre>\n<p>   3) \/\/ constructor pattern <\/p>\n<pre>\r\nfunction Fruit (theColor, theSweetness, theFruitName, theNativeToLand) {\r\n\r\n    this.color = theColor;\r\n    this.sweetness = theSweetness;\r\n    this.fruitName = theFruitName;\r\n    this.nativeToLand = theNativeToLand;\r\n\r\n    this.showName = function () {\r\n        console.log(\"This is a \" + this.fruitName);\r\n    }\r\n\r\n    this.nativeTo = function () {\r\n    this.nativeToLand.forEach(function (eachCountry)  {\r\n       console.log(\"Grown in:\" + eachCountry);\r\n        });\r\n    }\r\n\r\n\r\n}\r\n\r\nvar mangoFruit = new Fruit (\"Yellow\", 8, \"Mango\", [\"South America\", \"Central America\", \"West Africa\"]);\r\n   <\/pre>\n<p>    4) \/\/ using prototype pattern <\/p>\n<pre>\r\nfunction Fruit () {\r\n\r\n}\r\n\r\nFruit.prototype.color = \"Yellow\";\r\nFruit.prototype.sweetness = 7;\r\nFruit.prototype.fruitName = \"Generic Fruit\";\r\nFruit.prototype.nativeToLand = \"USA\";\r\n\r\nFruit.prototype.showName = function () {\r\nconsole.log(\"This is a \" + this.fruitName);\r\n}\r\n\r\nFruit.prototype.nativeTo = function () {\r\n            console.log(\"Grown in:\" + this.nativeToLand);\r\n}\r\n\r\nvar mangoFruit = new Fruit ();\r\n   <\/pre>\n<p>    5)  \/\/ using Object.create()<\/p>\n<pre>\r\nvar superHuman = {\r\n    usePower: function () {\r\n        console.log(this.superPower + \"!\");\r\n    }\r\n};\r\n\r\nvar banshee = Object.create(superHuman, {\r\n    name: { value: \"Silver Banshee\" },\r\n    superPower: { value: \"sonic wail\" }\r\n});\r\n\r\n\/\/ Outputs: \"sonic wail!\"\r\nbanshee.usePower();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1) \/\/ object constructor var mango = new Object (); mango.color = &#8220;yellow&#8221;; mango.shape= &#8220;round&#8221;; mango.sweetness = 8; mango.howSweetAmI = function () { console.log(&#8220;Hmm Hmm Good&#8221;); } 2) \/\/ object literal \/\/ This is an object with 4 items, again using object literal var mango = { color: &#8220;yellow&#8221;, shape: &#8220;round&#8221;, sweetness: 8, howSweetAmI: function &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=694\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Way to create an object in Javascript<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=694"}],"version-history":[{"count":1,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":695,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/694\/revisions\/695"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}