Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Last active August 29, 2015 14:24
Show Gist options
  • Save mxriverlynn/5cfa341cfb548547c388 to your computer and use it in GitHub Desktop.
Save mxriverlynn/5cfa341cfb548547c388 to your computer and use it in GitHub Desktop.
the problem with options.change && options.change();
options.change && options.change();
if (options.change){ options.change(); }
// or
if ("change" in options) { options.change(); }
// or
if (Object.hasOwnProperty.call(options, "change")) { options.change(); }
// or ...
var options = {
foo: "bar"
};
if (_.isFunction(options.change)){
options.change();
}
// explicit separation of options and options persistence
function doStuff(options, optionsPersistence){
options.set("something", "that value");
options.set("whatever", 1234.1234);
// assume the save method is there, because that's what
// the API "contract" or "protocol" says should be there
if (optionsPersistence) {
optionsPersistence.save(options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment