{"id":531,"date":"2022-08-30T15:02:54","date_gmt":"2022-08-30T15:02:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/a-javascript-design-pattern-for-options-with-default-values-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:54","modified_gmt":"2022-08-30T15:02:54","slug":"a-javascript-design-pattern-for-options-with-default-values-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/a-javascript-design-pattern-for-options-with-default-values-collection-of-common-programming-errors\/","title":{"rendered":"A javascript design pattern for options with default values?-Collection of common programming errors"},"content":{"rendered":"<p>I think you&#8217;re looking for something like this (sorry for the late reply):<\/p>\n<pre><code>function foo(a, b, options) { \n    this.defaults = {\n        x: 48, \n        y: 72,\n        z: 35\n    };\n    for (var i in this.defaults) {\n        if (options[i] != \"undefined\") { this.defaults[i] = options[i]; }\n    }\n    \/\/ more code...\n}\n<\/code><\/pre>\n<p>edit: apologies, grabbed this from some old code&#8230; You should make sure to use the hasOwnProperty() method to make sure you don&#8217;t iterate over everything on function.prototype<\/p>\n<p>https:\/\/developer.mozilla.org\/en-US\/docs\/JavaScript\/Reference\/Global_Objects\/Object\/hasOwnProperty<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:19:26. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I think you&#8217;re looking for something like this (sorry for the late reply): function foo(a, b, options) { this.defaults = { x: 48, y: 72, z: 35 }; for (var i in this.defaults) { if (options[i] != &#8220;undefined&#8221;) { this.defaults[i] = options[i]; } } \/\/ more code&#8230; } edit: apologies, grabbed this from some old [&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-531","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/531","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=531"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}