{"id":397,"date":"2022-08-30T15:00:40","date_gmt":"2022-08-30T15:00:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/working-with-optional-arguments-in-javascript-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:00:40","modified_gmt":"2022-08-30T15:00:40","slug":"working-with-optional-arguments-in-javascript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/working-with-optional-arguments-in-javascript-collection-of-common-programming-errors\/","title":{"rendered":"Working with optional arguments in JavaScript-Collection of common programming errors"},"content":{"rendered":"<p>I created a function like this:<\/p>\n<pre><code>window.SetUploader = function(action, elementID, multiple, allowedExtensions) {\n    \/\/Optional arguments\n    if (actionUploader == null) {\n      actionUploader = \"\/Upload\";\n    }\n    if (elementIDUploader == null) {\n      elementIDUploader = \"file-uploader\";\n    }\n    if (multipleUploader == null) {\n      multipleUploader = false;\n    }\n    if (allowedExtensions == null) {\n      allowedExtensions = [];\n    }\n\n    \/\/Function\n     .....\n};\n<\/code><\/pre>\n<p>To call this function, use the following code:<\/p>\n<pre><code>SetUploader('\/Projects\/ImageUpload', 'Logo', { allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] });\n<\/code><\/pre>\n<p>Note the parameter <code>allowedExtensions<\/code>, it does not change the value.<\/p>\n<p>Debugging function, <code>allowedExtensions<\/code> value is <strong>null<\/strong>.<\/p>\n<p><strong>What is wrong?<\/strong><\/p>\n<ol>\n<li>\n<p>You are passing your actual <code>allowedExtendsions<\/code> parameter as the third argument which is where the <code>multiple<\/code> formal parameter appears. Put <code>undefined<\/code> before it.<\/p>\n<pre><code>SetUploader(\n   \/* action *\/    '\/Projects\/ImageUpload', \n   \/* elementId *\/ 'Logo', \n   \/\/ NEED VALUE FOR MULTIPLE HERE\n   ['jpg', 'jpeg', 'png', 'gif', 'zip'])\n<\/code><\/pre>\n<p>You are also never using the <code>action<\/code> parameter, instead checking and setting a global variable <code>actionUploader<\/code>.<\/p>\n<p>Typically when allowing optional arguments you have one particular argument which is a map of extra arguments.<\/p>\n<pre><code>function (requiredParam, anotherRequiredParam, extra) {\n  var optionalParam = extra ? extra.optionalParamName : defaultValue;\n  ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You are passing your object as third parameter (as multiple), your allowedExtensions is fourth.<\/p>\n<p>Also you are checking variables with &#8220;Uploader&#8221; as name suffix. They are not variables that you pass.<\/p>\n<p>Next, if you would pass <code>{ allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] }<\/code> as fourth parameter to get hold of array with extensions you would have to write <code>allowedExtensions.allowedExtensions<\/code><\/p>\n<\/li>\n<li>\n<p>First of all your variables are not null, they are undefined and you need to check with the <code>===<\/code> operator. undefined and null are not the same, variables in JavaScript that have the declared but not initialized have the value undefined. With the <code>===<\/code> operator you make JavaScript check the type and the values you are comparing. If you only use the <code>==<\/code> operator JavaScript will attempt to type convert your variables making this:<\/p>\n<pre><code>if (allowedExtensionsUploader == null)\n<\/code><\/pre>\n<p>into this:<\/p>\n<pre><code>if (false == false)\n<\/code><\/pre>\n<p>which can lead to problems.<\/p>\n<p>Secondly you only provide three arguments to your function but you&#8217;re asking about the fourth.<\/p>\n<p>Thirdly, the fourth argument is called <strong>allowedExtensions<\/strong> but you are checking a variable called <strong>allowedExtensionsUploader<\/strong>.<\/p>\n<p>Fourthly, I think you should use jslint to check your code. It would have told you a lot of what I just wrote =)<\/p>\n<\/li>\n<li>\n<p>Since you are passing it as the parameter <code>multiple<\/code>, as it is the <strong>third parameter<\/strong>.<\/p>\n<p><strong>In JavaScript you cannot have named parameters.<\/strong><\/p>\n<pre><code> { allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] }\n<\/code><\/pre>\n<p>Is an object that has a property <code>allowedExtensions<\/code> init to an array.<\/p>\n<\/li>\n<li>\n<p>you are passing only three arguments in the function call<\/p>\n<p>the parameter<\/p>\n<pre><code>{ allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] }\n<\/code><\/pre>\n<p>is a JSON object that will be available in &#8220;multiple&#8221; variable<\/p>\n<p>Try adding one more argument in the function call, you will get that value as the 4th parameter &#8220;allowedExtensions&#8221;<\/p>\n<\/li>\n<li>\n<ol>\n<li>Your arguments do not match your variables in your if statements.<\/li>\n<li>You should check whether the variables are <code>undefined<\/code>, not <code>null<\/code>.<\/li>\n<li>You should use <code>===<\/code>, not <code>==<\/code>.<\/li>\n<\/ol>\n<p>Use this instead:<\/p>\n<pre><code>window.SetUploader = function(actionUploader, elementIDUploader, multipleUploader, allowedExtensionsUploader) {\n    \/\/Optional arguments\n    if (actionUploader === undefined) {\n      actionUploader = \"\/Upload\";\n    }\n    if (elementIDUploader === undefined) {\n      elementIDUploader = \"file-uploader\";\n    }\n    if (multipleUploader === undefined) {\n      multipleUploader = false;\n    }\n    if (allowedExtensionsUploader === undefined) {\n      allowedExtensionsUploader = [];\n    }\n};\n\nSetUploader('\/Projects\/ImageUpload', 'Logo', undefined, ['jpg', 'jpeg', 'png', 'gif', 'zip']);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 18:45:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I created a function like this: window.SetUploader = function(action, elementID, multiple, allowedExtensions) { \/\/Optional arguments if (actionUploader == null) { actionUploader = &#8220;\/Upload&#8221;; } if (elementIDUploader == null) { elementIDUploader = &#8220;file-uploader&#8221;; } if (multipleUploader == null) { multipleUploader = false; } if (allowedExtensions == null) { allowedExtensions = []; } \/\/Function &#8230;.. }; To [&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-397","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/397","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=397"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/397\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}