Skip to content

Instantly share code, notes, and snippets.

@psiborg
Created January 6, 2012 08:17
Show Gist options
  • Select an option

  • Save psiborg/1569649 to your computer and use it in GitHub Desktop.

Select an option

Save psiborg/1569649 to your computer and use it in GitHub Desktop.
JS Object Defaults 2
DEFAULTS = {
color: "blue",
border: 0,
width: '128px',
height: '128px'
};
function mergeOptions (defaults, options) {
for (var name in defaults) {
if (defaults.hasOwnProperty(name)) {
if (options[name] == null) {
options[name] = defaults[name];
}
}
}
return options;
}
function drawElement (options) {
var options = mergeOptions(DEFAULTS, options);
console.log(options);
}
drawElement({color: "red"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment