A javascript design pattern for options with default values?-Collection of common programming errors
I think you’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] != "undefined") { this.defaults[i] = options[i]; }
}
// more code...
}
edit: apologies, grabbed this from some old code… You should make sure to use the hasOwnProperty() method to make sure you don’t iterate over everything on function.prototype
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
Originally posted 2013-11-09 20:19:26.