Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is our method for doing optional parameters in javascript.
- // In this example source is a mandatory parameter, opts are optional,
- // and callback is an optional callback. Optional parameters are stored
- // in the object opts.
- const myfunction = (source, opts, callback) => {
- // check if any optional parameters were provided by looking at the type of the second argument
- if (typeof opts === 'function') {
- // If opts is a function it's the callback
- callback = opts;
- // Set opts to be an empty object
- opts = {};
- }
- // Then create consts for all parameters, containing
- // either the value in opts, or the default value.
- // So this is where you providde default values.
- const params = opts && opts.params ? opts.params : {};
- const options = opts && opts.options ? opts.options : {};
- const commitHash = opts && opts.commitHash ? opts.commitHash : 'master';
- // do stuff
- if (callback) callback(null);
- };
Add Comment
Please, Sign In to add comment