Guest User

Untitled

a guest
Feb 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function connectToDb() {
  2. console.log('made a connection to database');
  3. return "dbConnection";
  4. }
  5.  
  6. var connection = connectToDb();
  7. var dbQueryWithoutClosure = function (queryParam) {
  8. // var connection = connectToDb();
  9. console.log('request without closure param: ' + queryParam + ' to db with connection: ' + connection);
  10. }
  11.  
  12. dbQueryWithoutClosure("a");
  13. dbQueryWithoutClosure("b");
  14. dbQueryWithoutClosure("c");
  15.  
  16.  
  17. function dbQueryFactory() {
  18. var conn = connectToDb();
  19. return function (queryParam) {
  20. console.log('request with closure param: ' + queryParam + ' to db with connection: ' + conn);
  21. };
  22. }
  23.  
  24. var dbQueryWithClosure = dbQueryFactory();
  25. dbQueryWithClosure("a");
  26. dbQueryWithClosure("b");
  27. dbQueryWithClosure("c");
Add Comment
Please, Sign In to add comment