{"id":1633,"date":"2016-12-01T13:23:11","date_gmt":"2016-12-01T13:23:11","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=1633"},"modified":"2017-11-16T23:15:00","modified_gmt":"2017-11-16T23:15:00","slug":"javascript-lesson-34-creating-object-using-constructor-notation","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/","title":{"rendered":"JavaScript Lesson 34: Creating an Object using Constructor Notation"},"content":{"rendered":"<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<\/strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\">\u00a0&lt;Next Lesson&gt;<\/a><\/strong><\/h3>\n<p>We can create an object in JavaScript using the New keyword and the Object() constructor function. Bear in mind the O of the Object function must be a capital letter, otherwise it will not work.<\/p>\n<p>After creating the Object, you can then add properties and method to it using the dot notation. Please ensure that each element that adds a property or method should end with a semicolon.<\/p>\n<h3>Example: A Car Object<\/h3>\n<p><strong><span style=\"color: #0000ff;\">The JavaScript<\/span><\/strong><\/p>\n<pre style=\"font-size: 100%;\"><span style=\"color: #0000ff;\">var<\/span> car=<span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #800080;\">Object<\/span><span style=\"color: #0000ff;\">();<\/span>\r\ncar.brand='Toyota'<span style=\"color: #0000ff;\">;<\/span>\r\ncar.model='Camry'<span style=\"color: #0000ff;\">;<\/span>\r\ncar.capacity='2000cc'<span style=\"color: #0000ff;\">;<\/span>\r\ncar.stock=10000<span style=\"color: #0000ff;\">;<\/span>\r\ncar.booked=3000<span style=\"color: #0000ff;\">;<\/span>\r\ncar.checkBalance=<span style=\"color: #0000ff;\">function()<\/span>{\r\n<span style=\"color: #0000ff;\">return this<\/span>.stock-<span style=\"color: #0000ff;\">this<\/span>.booked<span style=\"color: #0000ff;\">;<\/span>\r\n};\r\n<span style=\"color: #0000ff;\">var<\/span> carbrand=<span style=\"color: #993300;\">document<\/span>.getElementById<span style=\"color: #0000ff;\">(<\/span>'carbrand<span style=\"color: #0000ff;\">');<\/span>\r\ncarbrand.textContent=car.brand;\r\n<span style=\"color: #0000ff;\">var<\/span> carmodel=<span style=\"color: #993300;\">document<\/span>.getElementById<span style=\"color: #0000ff;\">(<\/span>'carmodel'<span style=\"color: #0000ff;\">);<\/span>\r\ncarmodel.textContent=car.model;\r\n<span style=\"color: #0000ff;\">var<\/span> carbalance=<span style=\"color: #993300;\">document<\/span>.getElementById<span style=\"color: #0000ff;\">(<\/span>'balance'<span style=\"color: #0000ff;\">);<\/span>\r\ncarbalance.textContent=car.checkBalance<span style=\"color: #0000ff;\">();<\/span>\r\n<\/pre>\n<p>In this example, checkBalance is a method, it is actually a function by itself.<\/p>\n<p>The corresponding HTML document<\/p>\n<pre style=\"font-size: 100%;\">&lt;!DOCTYPE html&gt;\r\n<span style=\"color: #0000ff;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #0000ff;\">&lt;body&gt;<\/span>\r\n<span style=\"color: #0000ff;\">&lt;h1<\/span>&gt;Car brand:<span style=\"color: #0000ff;\">&lt;b<\/span><span style=\"color: #ff0000;\"> id<\/span>=<span style=\"color: #3366ff;\">\"carbrand\"<\/span>&gt;<span style=\"color: #0000ff;\">&lt;\/b&gt;&lt;\/h1&gt;<\/span>\r\n<span style=\"color: #0000ff;\">&lt;h1&gt;<\/span>Car Model:<span style=\"color: #0000ff;\">&lt;b<\/span><span style=\"color: #ff0000;\"> id<\/span>=\"carmodel\"&gt;<span style=\"color: #0000ff;\">&lt;\/b&gt;&lt;\/h1&gt;<\/span>\r\n<span style=\"color: #0000ff;\">&lt;h2&gt;<\/span>Number of Cars Left:<span style=\"color: #0000ff;\">&lt;b<\/span><span style=\"color: #ff0000;\"> id<\/span>=<span style=\"color: #3366ff;\">\"balance\"<\/span>&gt;<span style=\"color: #0000ff;\">&lt;\/b&gt;&lt;\/h2&gt;<\/span>\r\n<span style=\"color: #0000ff;\">&lt;script<\/span><span style=\"color: #ff0000;\"> type<\/span>=<span style=\"color: #3366ff;\">\"text\/javascript\"<\/span> <span style=\"color: #ff0000;\">src<\/span>=<span style=\"color: #3366ff;\">\"car.js\"<\/span>&gt;<span style=\"color: #0000ff;\">&lt;\/script&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #0000ff;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>The Output<\/p>\n<h1>Car brand:<b id=\"carbrand\"><\/b><\/h1>\n<h1>Car Model:<b id=\"carmodel\"><\/b><\/h1>\n<h2>Number of Cars Left:<b id=\"balance\"><\/b><\/h2>\n<p><script>\nvar car=new Object();\ncar.brand='Toyota';\ncar.model='Camry';\ncar.capacity='2000cc';\ncar.stock=10000;\ncar.booked=3000;\ncar.checkBalance=function(){\nreturn this.stock-this.booked;\n};\nvar carbrand=document.getElementById('carbrand');\ncarbrand.textContent=car.brand;\nvar carmodel=document.getElementById('carmodel');\ncarmodel.textContent=car.model;\nvar carbalance=document.getElementById('balance');\ncarbalance.textContent=car.checkBalance();<\/script><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- Javascript Responsive Ads --><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"9005101105\"\n     data-ad-format=\"auto\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<\/strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/creating-animation-javascript\/\">\u00a0&lt;Next Lesson&gt;<\/a><\/strong><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0\u00a0&lt;Next Lesson&gt; We can create an object in JavaScript using the New keyword and the Object() constructor function. Bear in mind the O of the Object function must be a capital letter, otherwise it will not work. After creating the Object, you can then add properties and method to it using the &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 34: Creating an Object using Constructor Notation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1633","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 34: Creating an Object using Constructor Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 34: Creating an Object using Constructor Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0\u00a0&lt;Next Lesson&gt; We can create an object in JavaScript using the New keyword and the Object() constructor function. Bear in mind the O of the Object function must be a capital letter, otherwise it will not work. After creating the Object, you can then add properties and method to it using the &hellip; Continue reading &quot;JavaScript Lesson 34: Creating an Object using Constructor Notation&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T23:15:00+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\",\"name\":\"JavaScript Lesson 34: Creating an Object using Constructor Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2016-12-01T13:23:11+00:00\",\"dateModified\":\"2017-11-16T23:15:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 34: Creating an Object using Constructor Notation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 34: Creating an Object using Constructor Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 34: Creating an Object using Constructor Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0\u00a0&lt;Next Lesson&gt; We can create an object in JavaScript using the New keyword and the Object() constructor function. Bear in mind the O of the Object function must be a capital letter, otherwise it will not work. After creating the Object, you can then add properties and method to it using the &hellip; Continue reading \"JavaScript Lesson 34: Creating an Object using Constructor Notation\"","og_url":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T23:15:00+00:00","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/","url":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/","name":"JavaScript Lesson 34: Creating an Object using Constructor Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2016-12-01T13:23:11+00:00","dateModified":"2017-11-16T23:15:00+00:00","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 34: Creating an Object using Constructor Notation"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=1633"}],"version-history":[{"count":11,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1633\/revisions"}],"predecessor-version":[{"id":2512,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1633\/revisions\/2512"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}