{"id":5866,"date":"2022-06-29T04:32:15","date_gmt":"2022-06-29T04:32:15","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5866"},"modified":"2022-06-29T04:32:15","modified_gmt":"2022-06-29T04:32:15","slug":"constructor-design-pattern","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/","title":{"rendered":"Constructor design pattern"},"content":{"rendered":"<p>In Object-oriented programming, a constructor is a method used to initialize a newly created object after memory has been allocated for that object. In javascript, almost everything is an object. Object constructor is one of the most important topics that we may be interested in. <\/p>\n<p>To create specific types of objects object constructor is used. Preparing the object for use and accepting arguments a constructor can be used to set the values of properties and methods when the object is created. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/06\/desgin.png\" alt=\"desgin\"\/><\/p>\n<h2>Object Creation<\/h2>\n<p>There are multiple ways to create javascript objects.<br \/>\n<code>Var obj = {};<br \/>\nVar obj = new Object();<\/code><\/p>\n<p>The Object constructor creates an object wrapper for a particular value. If no value is passed it will create an empty object.<br \/>\nThere are four ways to pass keys and values to an object.<br \/>\n<code>obj.name = \u2018Sunil Sethi\u2019;<br \/>\nvar key = obj.name;<\/p>\n<p>Obj[\u2018name\u2019] = \u2018Sunil Sethi\u2019;<br \/>\nVar key  = obj[\u2018name\u2019];<\/p>\n<p>Object.defineProperty(obj, \u201cname\u201d, {<br \/>\n\tValue : \u201csunil sethi\u201d,<br \/>\n\tWritable:true,<br \/>\n\tEnumerable : true,<br \/>\n\tConfigurable: true,<br \/>\n});<\/p>\n<p>Object.defineProperties(obj,{<br \/>\n    \"name\":\"sunil\",<br \/>\n     writable:true,<br \/>\n    \"age\":30,<br \/>\n    writable:false,<br \/>\n  });<\/code><\/p>\n<h3>Basic Constructor:<\/h3>\n<p>As Javascript doesn&#8217;t support the concept of classes, it supports the constructor function that works with objects. We can just prefix a call to a constructor function with the <strong><em>new <\/em><\/strong>keyword. We can make the function behave like a constructor and instantiate a new object with the members defined by that function. Under the constructor, the keyword this references the new object that is being created. The basic look of the constructor is as follows. <\/p>\n<p><code>function Display(name, age, roll){<br \/>\n    this.name = name;<br \/>\n    this.age= age;<br \/>\n    this.roll = roll;<br \/>\n    this.toString = function (){<br \/>\n        return this.name  + this.age + this.roll;<br \/>\n    }<br \/>\n}<br \/>\nvar display = new Display(\"sunil\",32,101);<br \/>\nconsole.log(display.toString());<\/code><\/p>\n<p>The above is the simplest version of the constructor pattern, but it has some problems. <\/p>\n<ul>\n<li>It makes inheritance difficult<\/li>\n<li>Functions such as toString are redefined for each new object created using the Display constructor.<\/li>\n<\/ul>\n<p>As there are alternative methods available in both ES3 to ES6 to construct objects. <\/p>\n<h3>Constructor with Prototypes<\/h3>\n<p>All the functions in javascript have a property which is called a prototype. When we call a constructor to create objects. Every property of the constructor prototype is available to the new object. In this approach, multiple objects can be created that can access the same prototype. For example.<br \/>\n<code>function Display(name,age,roll){<br \/>\n    this.name = name;<br \/>\n    this.age = age;<br \/>\n    this.roll =roll;<br \/>\n}<br \/>\nDisplay.prototype.toString = function (){<br \/>\n    return this.name + this.age + this.roll;<br \/>\n}<br \/>\nvar mca = new Display(\"sunil\",32,102);<br \/>\nvar bca = new Display(\"Bhagwant maan\",49,202);<br \/>\nconsole.log(bca.toString());<br \/>\nconsole.log(mca.toString());<\/code><\/p>\n<p><small><em>StudySection has a long list of certification exams that it offers through its online platform. The <a href=\"https:\/\/www.studysection.com\/cakephp-4.x-advanced\">PHP Certification Exam<\/a> is one of the programming certifications that it provides. Whether you are new to PHP programming or you have extensive experience in PHP programming, you can get a certification according to your level. Attach a PHP certification with your resume to get the most out of job offers.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Object-oriented programming, a constructor is a method used to initialize a newly created object after memory has been allocated<\/p>\n","protected":false},"author":1,"featured_media":5867,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[142,789],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Constructor design pattern - StudySection Blog<\/title>\n<meta name=\"description\" content=\"In Object-oriented, a constructor is a method used to initialize a newly created object after memory has been allocated for that object.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructor design pattern - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"In Object-oriented, a constructor is a method used to initialize a newly created object after memory has been allocated for that object.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-29T04:32:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/06\/Constructor-design.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Constructor design pattern\",\"datePublished\":\"2022-06-29T04:32:15+00:00\",\"dateModified\":\"2022-06-29T04:32:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\"},\"wordCount\":372,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"C\",\"Constructor\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\",\"url\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\",\"name\":\"Constructor design pattern - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-06-29T04:32:15+00:00\",\"dateModified\":\"2022-06-29T04:32:15+00:00\",\"description\":\"In Object-oriented, a constructor is a method used to initialize a newly created object after memory has been allocated for that object.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Constructor design pattern\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constructor design pattern - StudySection Blog","description":"In Object-oriented, a constructor is a method used to initialize a newly created object after memory has been allocated for that object.","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:\/\/studysection.com\/blog\/constructor-design-pattern\/","og_locale":"en_US","og_type":"article","og_title":"Constructor design pattern - StudySection Blog","og_description":"In Object-oriented, a constructor is a method used to initialize a newly created object after memory has been allocated for that object.","og_url":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-06-29T04:32:15+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/06\/Constructor-design.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Constructor design pattern","datePublished":"2022-06-29T04:32:15+00:00","dateModified":"2022-06-29T04:32:15+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/"},"wordCount":372,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["C","Constructor"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/constructor-design-pattern\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/","url":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/","name":"Constructor design pattern - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-06-29T04:32:15+00:00","dateModified":"2022-06-29T04:32:15+00:00","description":"In Object-oriented, a constructor is a method used to initialize a newly created object after memory has been allocated for that object.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/constructor-design-pattern\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/constructor-design-pattern\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Constructor design pattern"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":151,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5866"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5866"}],"version-history":[{"count":1,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5866\/revisions"}],"predecessor-version":[{"id":5869,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5866\/revisions\/5869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5867"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}