{"id":535,"date":"2022-08-30T15:02:58","date_gmt":"2022-08-30T15:02:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-define-optional-parameters-in-java-script-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:58","modified_gmt":"2022-08-30T15:02:58","slug":"how-to-define-optional-parameters-in-java-script-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-define-optional-parameters-in-java-script-collection-of-common-programming-errors\/","title":{"rendered":"How to define optional parameters in Java Script-Collection of common programming errors"},"content":{"rendered":"<p>Here is my object construction,<\/p>\n<pre><code>function Employee(name, dob) {\n       this.Name = name;\n       this.DateOfBirth = new Date(dob);\n   }\n<\/code><\/pre>\n<p>Now, I have created an instance for this, like<\/p>\n<pre><code>var emp = new Employee(\"sample\",\"12\/12\/12\");\n<\/code><\/pre>\n<p>Its working fine when i print the output.<\/p>\n<p>But, if i create the object like<\/p>\n<pre><code>var emp = new Employee(name = \"sample\");\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>var emp = new Employee(dob = \"12\/12\/12\");\n<\/code><\/pre>\n<p>its not working fine. In both the cases, the <code>DateOfBirth<\/code> field is invalid.<\/p>\n<p>I need to define an object with optional parameters.<\/p>\n<ol>\n<li>\n<p>JavaScript does not support named optional parameters.<\/p>\n<p>When you do <code>var emp = new Employee(name = \"sample\");<\/code><\/p>\n<p>You&#8217;re declaring a <code>name<\/code> global variable, assigning <code>sample<\/code> to it and passing <em>that<\/em> to the <code>new<\/code> call.<\/p>\n<p>You can use objects to accomplish similar syntax in JS:<\/p>\n<pre><code>var emp = new Employee({name:\"sample\"}); \n<\/code><\/pre>\n<p>Where the Employee function becomes:<\/p>\n<pre><code>function Employee(options) {\n       this.Name = options.name;\n       if(options.dob !== undefined){\n           this.DateOfBirth = new Date(options.dob);\n       }\n}\n<\/code><\/pre>\n<p>Worth mentioning, in practice, you often don&#8217;t need an Employee class, and can simply do:<\/p>\n<pre><code>var emp = {name:\"sample\"};\n<\/code><\/pre>\n<p>Or:<\/p>\n<pre><code>var emp = {dob:new Date(\"12\/12\/12\");}\n<\/code><\/pre>\n<p>So unless Employee grows to become a real model (and has more than just two fields) I think that you might want to consider that.<\/p>\n<\/li>\n<li>\n<pre><code>function Employee(params) {\n    if (typeof params != \"undefined\") {\n        this.Name = (typeof params.name != \"undefined\") ? params.name : \"\";\n        this.DateOfBirth = (typeof params.dob != \"undefined\") ? new Date(params.dob) : null;\n    }\n}\n\nnew Employee({\n    name: \"John\",\n    dob: \"12\/12\/12\"\n});\nnew Employee({\n    name: \"John\"\n});\nnew Employee({\n    dob: \"12\/12\/12\"\n});\n<\/code><\/pre>\n<p>or using simple statements using <code>||<\/code>.<\/p>\n<pre><code>function Employee(params) {\n    params = params || {};\n    this.Name = params.name || \"\";\n    this.DateOfBirth = new Date(params.dob || \"\");\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>As a good practice, you should never leave out variables. You can explicitly call <code>var emp = new Employee(null, \"12\/12\/12\");<\/code><\/p>\n<p>This way everything is initialized and you will not havea headaches later on. There is also something like this, but you really need to check the values before assigning.<\/p>\n<pre><code>function Employee() {\n       this.Name = name;\n       this.DateOfBirth = new Date(dob);\n   }\n\nvar emp = new Employee(name = null,dob = \"12\/12\/12\");\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:19:47. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here is my object construction, function Employee(name, dob) { this.Name = name; this.DateOfBirth = new Date(dob); } Now, I have created an instance for this, like var emp = new Employee(&#8220;sample&#8221;,&#8221;12\/12\/12&#8243;); Its working fine when i print the output. But, if i create the object like var emp = new Employee(name = &#8220;sample&#8221;); or var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}