Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. // This is our method for doing optional parameters in javascript.
  2. // In this example source is a mandatory parameter, opts are optional,
  3. // and callback is an optional callback. Optional parameters are stored
  4. // in the object opts.
  5. const myfunction = (source, opts, callback) => {
  6. // check if any optional parameters were provided by looking at the type of the second argument
  7. if (typeof opts === 'function') {
  8. // If opts is a function it's the callback
  9. callback = opts;
  10. // Set opts to be an empty object
  11. opts = {};
  12. }
  13. // Then create consts for all parameters, containing
  14. // either the value in opts, or the default value.
  15. // So this is where you providde default values.
  16. const params = opts && opts.params ? opts.params : {};
  17. const options = opts && opts.options ? opts.options : {};
  18. const commitHash = opts && opts.commitHash ? opts.commitHash : 'master';
  19.  
  20. // do stuff
  21.  
  22. if (callback) callback(null);
  23. };
Add Comment
Please, Sign In to add comment